I’m trying to create a string (foo2) which will have all the string object in the fooArray. I don’t understand why the below posted code doesn’t do that for me.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *foo =@"This is just a test";
NSArray *fooArray = [foo componentsSeparatedByString:@" "];
for(int i=0; i < fooArray.count; i++)
{
_foo2= [_foo2 stringByAppendingString:[ fooArray objectAtIndex:i]];
}
NSLog(_foo2);
}
I’m guessing it’s because you are not initializing
_foo2anywhere beforeviewDidLoadruns. And since _foo2 hasn’t been initialized and is still a nil pointer, every time you send thestringByAppendingString:message to it, the result is still nil.You can fix this by initializing _foo2 to a new NSMutableString object before your loop.