What is the syntax for checking if strings are identical?
in Java it is: string1.equals(string2);
but what is it in objective C?
What is the syntax for checking if strings are identical?
in Java it is: string1.equals(string2);
but what is it in objective C?
NSString *String1, *String2;
if([String1 compare: String2] == NSOrderedSame)
//They are the same
NSOrderedSame
is defined as zero, so you can write
if(![String1 compare: String2])
//Equals
Use the specific string equality message
[string1 isEqualToString: string2]
You need to use isEqualToString.
if ( [stringOne isEqualToString: stringTwo] ) { }
You'll need to use isEqualToString
for the most accurate results. I've included a couple examples on how to use it.
NSString *aString = foo;
NSString *bString = bar;
if ([aString isEqualToString:bString]) {
NSLog("Match");
}
else NSLog("No Match");
//No match.
NSString *aString = foo;
NSString *bString = bar;
if ([aString isEqualToString:@"foo"]) {
NSLog("Double Foo!");
}
else NSLog("No Match");
//Double Foo!
NSString *aString = foo;
NSString *bString = bar;
if (![aString isEqualToString:bString]) {
NSLog("No Match");
}
else NSLog("Match");
//No Match