Any one please help me to understand the String comparison technique in Objective-C
NSString *strNew1 = @"AA";
NSString *strNew2 = @"AA";
So to compare both the strings we could use,
Method 1. if (strNew1 == strNew2) {
NSLog(@"Equal");
}
or
Method 2: if ([strNew1 isEqualToString:strNew2]) {
NSLog(@"Equal");
}
In this condition both of them are success. But am aware that method 1 will get failed at certain other condition. And also I have tried the below conditions(All are success).
NSString *strNew = @"AA";
NSString *strNew1 = @"AA";
NSString *strNew11 = [[NSString alloc] initWithString:strNew1];
NSString *strNew3 = strNew;
NSArray *arr = @[@"AA"];
NSString *strNew4 = [arr objectAtIndex:0];
NSString *strNew5 = [arr objectAtIndex:0];
_test = strNew5;
_test1 = @"AA";
if ([strNew isEqualToString:strNew1]) {
NSLog(@"Equal");
}
if (strNew == strNew3) {
NSLog(@"Equal1");
}
if (strNew == [arr objectAtIndex:0]){
NSLog(@"Equal2");
}
if (strNew == strNew4){
NSLog(@"Equal3");
}
if (strNew5 == strNew4){
NSLog(@"Equal4");
}
if (strNew4 == [arr objectAtIndex:0]){
NSLog(@"Equal5");
}
if (strNew11 == [arr objectAtIndex:0]){
NSLog(@"Equal11");
}
if (self.test == strNew4){
NSLog(@"Equal3");
}
if (self.test == self.test1){
NSLog(@"Equal3");
}
TEST *test = [TEST new]; // Tried with a class with NSString property with value "AA" . (test.strTest value is @"AA")
if (strNew == test.strTest) {
NSLog(@"Equal"); //success
}
I knew most of them are redundant. Am not able to understand the basics behind this. Please anyone give clear explanation on the concept behind this. Thanks.