Casting NSArray-item to NSString?

Discussion in 'iOS Programming' started by Danneman101, Mar 6, 2009.

  1. Danneman101 macrumors 6502

    Aug 14, 2008
    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?
  2. jnic macrumors 6502a

    Oct 24, 2008
    What errors are you seeing (build warnings and console)?
  3. Danneman101 thread starter macrumors 6502

    Aug 14, 2008
    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);
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.

Share This Page