如何测试2个NSString以查看它们是否相同?

The*_*mer 0 objective-c nsstring

所以我想测试2 1NSString,看看它们在我输入时是否相同:

NSString *theOriginalString = [NSString stringWithFormat:@"Superman"];

NSString *theTypedString = [textView string];
Run Code Online (Sandbox Code Playgroud)

我想在输入时查看是否TypedString有错,如果有人输入了错误的答案,则会弹出警告.

先感谢您.

Jos*_*hua 5

用于isEqualToString:确定两个字符串是否相同,在您的情况下执行以下操作:

if ([theOriginalString isEqualToString:theTypedString] == NO) {
    NSLog(@"The Strings are Different, wrong answer!");
} else {
    NSLog(@"The Strings are the Same, correct answer!");
}
Run Code Online (Sandbox Code Playgroud)

编辑

如果你想确保他们输入的内容是正确的,试试这个:

if ([theOriginalString hasPrefix:theTypedString] == NO) {
    NSLog(@"The Strings are Different, wrong answer!");
} else {
    NSLog(@"The Strings are the Same, correct answer!");
}
Run Code Online (Sandbox Code Playgroud)