views:

41

answers:

1

So I want to test 2 NSString to see if they're the same while I'm typing so like:

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

NSString *theTypedString = [textView string];

I want to see if theTypedString is wrong while I type it out so a warning pops out if someone typed the wrong answer.

Thank you in advance.

+2  A: 

Use isEqualToString: to figure out if two strings are the same, in you case do the following:

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

EDIT

If you want to make sure what they have typed so far is right, try this:

if ([theOriginalString hasPrefix:theTypedString] == NO) {
    NSLog(@"The Strings are Different, wrong answer!");
} else {
    NSLog(@"The Strings are the Same, correct answer!");
}
Joshua
Thank You. That's almost what I wanted. What I actually want is to get the in front of the NSString part right. Think of this like typing an irrational number like Pi or E. By the way, does anyone knows where is the isEqualToString method defined?
theAmateurProgrammer
`isEqualToString:` is defined in the NSString Class Reference. http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html
Joshua
Also edited the answer to make sure what the user has typed so far is correct.
Joshua
The documentation really helped. The "hasPrefix:" method is the one I needed. Thanks a lot
theAmateurProgrammer