Search code examples
objective-cxcode4

Objective-C Changing values of instance variables in method


I am write a Objective-C Code on XCode 4.4. I have a NSMutableArray as a instance variable of my class k_info I have defined and synthesized (nonatomic,retain) property by the name of onesplaces too. I am unable to add a NSMUtableString object in the NSMutableArray onesplaces. When I try to add it.The size of onesplaces remains 0 and object at zero index obviously remains null. I tried doing this with and without using "self" key-word but it didnt worked in either case. My syntax of adding object and printing it is right because when I create a new NSMutableArray test and try to do the same thing it works for it but not for the instance variable onesplaces. I cannot proceed any further in my project without solving this issue.please tell me why is it happening and how should I solve this problem.

-(void)createinfo:(NSMutableArray )al varsis:(int)vars { NSMutableString stes=[[NSMutableString alloc]init]; stes=(NSMutableString*)@"string0";

  [ onesplaces addObject:stes];
  NSLog(@"%u",[onesplaces count]);
  NSLog(@"value is: %@ ",[ onesplaces objectAtIndex:0]);

  [ self.onesplaces addObject:stes];
   NSLog(@"%u",[onesplaces count]);
   NSLog(@"value is: %@ ",[self.onesplaces objectAtIndex:0]);

  NSMutableArray* test=[[NSMutableArray alloc]init];
  [ test addObject:stes];
   NSLog(@"%u",[test count]);
  NSLog(@"value is: %@ ",[test objectAtIndex:0]);

}

Solution

  • You probably forgot to create the array. Somewhere in your code, maybe in your init method, you need to create the array before using it.

    self.onesplaces = [[NSMutableArray alloc] init];
    

    You get nil instead of error messages because Objective-C allows you to send messages to nil, which always return nil.