Converting NSString to NSarray using componentsSeperatedByString problem
First off I would like to thank everyone for attempting to solve my problem. I am kind of a noobie so any help will be appreciated.
I have declared (NSString *data
) that has retrieved content from a website. I have tested through various parts of my code that the data is in the string and that it is proper.
The data in the string is EXACTLY as follows:
blah 2222, 3333, 1111 222 444 5555,<br/> blah 3333, 4444, 2222 333 555, <br/> blah 4444, 5555, 3333 444 666
this pattern continues for a while. I want to parse this data in a array so it is separated by ",". Therefore; I declared an array called (NSArray *storage
) and I will then do:
storage = [dat componentsSeperatedByString:","];
I then have a label called (UILabel *label
). It is connected through the interface builder, the property is set as well as synthesize.
I then go:
label.text = [storage objectAtIndex:0];
I run the Iphone simulator and when i press the button in my app which will activate the change, the iphone simulato开发者_JS百科r exists to the home screen where all the apps are located.
Why is this happening? why isn't it displaying blah 2222
?
Thanks!
componentsSeparatedByString:
returns an autoreleased array. Your storage
array is probably already deallocated when you tap the button. Use a retained property for the array instead of assigning it directly to the instance variable.
you can use this piece of code:
NSString *data = @"blah 2222, 3333, 1111 222 444 5555,<br/> blah 3333, 4444, 2222 333 555, <br/> blah 4444, 5555, 3333 444 666";
NSArray *storage = [data componentSeparatedBy:@","];
NSString *first= [data objectAtIndex:0];
NSString *second = [data objectAtIndex:1];
///... and so on
//or
label.text = [data objectAtIndex:2];
精彩评论