PDA

View Full Version : Casting NSArray-item to NSString?




Danneman101
Mar 6, 2009, 11:19 AM
No matter how much I meddle with the syntax, I cant seem to cast an item in a NSArray to a NSString.

This is the code that makes the NSArray, by dividing up a string by a separator:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];


So, shouldnt it be an easy thing to extract, for instance, the first name to a new string?


NSString *singleName = [listItems objectAtIndex:0];


But it wont work, so Im guessing Im missing some sort of cast, or some other piece of syntax is wrong?



jnic
Mar 6, 2009, 11:25 AM
What errors are you seeing (build warnings and console)?

Danneman101
Mar 6, 2009, 11:39 AM
I get no warnings, but the app crashes and this line in main seem to be the culprit:


int retVal = UIApplicationMain(argc, argv, nil, nil);

dejo
Mar 6, 2009, 11:44 AM
But it wont work, so Im guessing Im missing some sort of cast, or some other piece of syntax is wrong?
You're not missing any sort of cast and your syntax looks correct.

Try setting a breakpoint at this line:
NSString *singleName =[listItems objectAtIndex:0];
When you run it, if the debugger stops at this line but then the app crashes when you step over that line, you know that line is somehow the culprit. If, however, the app crashes before or sometime after it breaks at that line, then there is something else causing the crash.