UITextField and NSString don't like spaces?

Discussion in 'iOS Programming' started by ukdmbfan, Feb 23, 2009.

  1. ukdmbfan macrumors newbie

    Sep 17, 2008
    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)

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

    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. dejo Moderator


    Staff Member

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

    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