Casting NSArray-item to NSString?

Discussion in 'iPhone/iPad Programming' started by Danneman101, Mar 6, 2009.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    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:
    Code:
    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?

    Code:
    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. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    What errors are you seeing (build warnings and console)?
     
  3. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    I get no warnings, but the app crashes and this line in main seem to be the culprit:

    Code:
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You're not missing any sort of cast and your syntax looks correct.

    Try setting a breakpoint at this line:
    Code:
    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