PDA

View Full Version : NSString Problem!!!




flomll
Aug 3, 2011, 02:39 AM
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.

@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
Aug 3, 2011, 03:01 AM
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
Aug 4, 2011, 12:30 AM
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!!!