UITextField and NSString don't like spaces?

Discussion in 'iPhone/iPad Programming' started by ukdmbfan, Feb 23, 2009.

  1. macrumors newbie

    Joined:
    Sep 17, 2008
    #1
    I'm having a bit of a problem whereby anything entered in a UITextField that's passed into a method in a new view controller (which the app subsequently moves to) seems to crash the app if the entered text contains a space and I try and write that string to NSLog or to the new view controller somewhere.

    For example, I've got a view controller with: (pseudo-code)

    Code:
    UITextField *myTextField;
    
    [newViewController handleText:myTextField.text];
    And a new view controller with:

    Code:
    UILabel myLabel;
        
    - (void)handleText:(NSString *)myText {
         NSLog([NSString stringWithFormat:@"passed text: %@", myText]);
         myLabel.text = myText;
    }
    If the text I enter into the UITextField doesn't contain a space, everything works perfectly; but if I do enter a space weird things happen - the NSLog entry causes the app to crash, and setting the text of the label displays something weird, like "( )". I've tried using either %s or %S in place of %@, but then I just get really strange results of bizarre character strings.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I don't have much to say about the problem with the spaces in the textfield. I've never encountered anything like that. I.E. Spaces work fine in my textfields. But I have a suggestion for your use of NSLog.

    Instead
    Code:
    NSLog(@"passed text: %@", myText);
    should be sufficient. I.E. the stringWithFormat is extraneous.

    That's because %s is intended for null-terminated arrays of 8-bit unsigned characters and %S is for null-terminated arrays of 16-bit Unicode characters, neither of which I doubt you want in this case.
     

Share This Page