'Search' label text with UITextField

2019-09-16 09:40发布

问题:

I've got a UILabel and a UITextField. Each time the user enters text in the field, I need to check weather the textfield text exists in the label's text (basically I'm searching the label's text) . I was using

NSRange range = [sentenceRequestLabel.text rangeOfString:resultString];
if (range.location == NSNotFound) {
    NSLog(@"string was found");
} else {
    NSLog(@"string was not found");
}

to check (resultString being the textfield.text), but even if the label text in the text field is not even close to the label's text NSLog says "string was found". Any ideas what's wrong?

回答1:

if (range.location == NSNotFound) {
    NSLog(@"string was found");

means "If string was not found, then print it was found". That == should be a !=.