简体   繁体   English

如何在Objective-C中修复字符串?

[英]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, 但是如果他/她用空格键输入“ dog” <---是错误的,

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 和ID希望通过放置图片来通知用户答案不正确,该如何完成

You could use the method stringByTrimmingCharactersInSet to get rid of leading or trailing spaces: 您可以使用stringByTrimmingCharactersInSet方法来消除前导或尾随空格:

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. 您应该在Xcode帮助系统中对NSString进行搜索,并阅读NSString类参考。 There are tons of useful methods in the NSString class for doing things like this. NSString类中有很多有用的方法可用于执行此类操作。

I'm confused, because in your previous post I thought that the answer "q" was the correct answer. 我很困惑,因为在您以前的帖子中,我认为答案“ q”是正确的答案。 In your code above, anything but q would be correct. 在上面的代码中,除q之外的任何其他都是正确的。

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. 至于放置图像,最简单的操作可能是将已安装图像的图像视图放置在视图控制器中,但将其hidden属性设置为YES。 Then, when you decide the user has entered the correct answer, set the image view's hidden property to NO to reveal it. 然后,当您确定用户输入了正确的答案时,请将图像视图的hidden属性设置为NO以显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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