简体   繁体   中英

How to fix string in objective-c?

I'm having a problem with the correct answer

When the user types in lets say "dog" the answer is right!

But if s/he types in "dog " <--- with a spacebar its wrong,

how do i fix this:

Code:

- (IBAction)btncheck:(id)sender {


    if ([_textbox.text isEqualToString:@"q"]) {

        _keyboard.hidden = YES;
        _textXclear.hidden = YES;
    }
    else {
        // Was not correct.  Notify user, or just don't do anything  
    }

and id like to notify user that the answer was not correct by placing an image, how is that done

You could use the method stringByTrimmingCharactersInSet to get rid of leading or trailing spaces:

NSString *string = [_textbox.text stringByTrimmingCharactersInSet: whitespaceCharacterSet];
if (string isEqualToString: "q")
{
   //string is wrong? If so, display an error message.
}
else
{
  //string is correct, resign first responder
}

You should do a search on NSString in the Xcode help system and read the NSString class reference. There are tons of useful methods in the NSString class for doing things like this.

I'm confused, because in your previous post I thought that the answer "q" was the correct answer. In your code above, anything but q would be correct.

As far as placing an image, the easiest thing to do is probably to put an image view, with image installed, in your view controller, but set it's hidden property to YES. Then, when you decide the user has entered the correct answer, set the image view's hidden property to NO to reveal it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM