NSString Problem!!!

Discussion in 'iOS Programming' started by flomll, Aug 3, 2011.

  1. flomll macrumors newbie

    Jul 4, 2008
    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;
    @ 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];
  2. admanimal macrumors 68040

    Apr 22, 2005
    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.
  3. flomll thread starter macrumors newbie

    Jul 4, 2008
    Found the error!


    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!!!

Share This Page