Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

flomll

macrumors newbie
Original poster
Jul 4, 2008
4
0
Hy I am a beginner at Objective-C Programming. I declare a NSString @interface.

This member I would like to write by a method and read by a other method. But this won't work. The program kills.

This is a part of my code. I hope any one can help me.
Code:
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
	IBOutlet UITextField * _username;
	IBOutlet UITextField * _password;
	IBOutlet UIButton * _loginout;
	IBOutlet UIActivityIndicatorView * _activity;
	
	NSString * _token;	
}

- (IBAction)showInfo:(id)sender;
- (IBAction)logInOut:(id)sender;

@end

@ implementation 
.....

- (void)viewDidLoad {
  _token = [[NSString new] retain];
  _token = [_token stringByAppendingString: (NSString * )[json_header objectForKey: @"token"]];

  NSLog(@"%@", _token]; // Here all right!!
}				

//! Build the data string for the login.
/*!
 */
- (NSString *) buildLoginFormData
{
	assert (_token != nil);
			
	NSString * str = @"username=user1&password=1234&";

        // At this point the program kills
	//NSLog(@"Token: %@", _token);
	
	str = [str stringByAppendingString: _token];
	return str;
}

- (IBAction)logInOut:(id)sender 
{	
     NSString * data = [self buildLoginFormData];
}

.....
@end
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
First of all, the correct terminology to use is that you are declaring an instance variable of type NSString.

You should make sure that you understand precisely what each line of your viewDidLoad method does, especially in terms of memory management.

The second assignment to _token is overwriting the first assignment with an autoreleased object which you fail to retain and therefore can no longer use once the method exits.
 

flomll

macrumors newbie
Original poster
Jul 4, 2008
4
0
Found the error!

Hy,

thanks for the tip! I found the mistake in the code. I read the whole NSString Reference at the Apple developer side.

Now I am not in front of my mac so I can not post the correct code. But I would like to do this at the evening.

Thanks for your help!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.