Understanding NSString comparison

后端 未结 7 1622
情歌与酒
情歌与酒 2020-11-22 12:15

Both the following comparisons evaluate to true:

1)

@\"foo\" == @\"foo\";

2)

NSString *myString1 = @\"foo\";
NSStri         


        
相关标签:
7条回答
  • 2020-11-22 13:12

    Check out this example:

    NSString *myString1 = @"foo";
    NSMutableString *myString2 = [[NSMutableString stringWithString:@"fo"] stringByAppendingString: @"o"];
    
    NSLog(@"isEquality: %@", ([myString1 isEqual:myString2]?@"+":@"-")); //YES
    NSLog(@"isEqualToStringity: %@", ([myString1 isEqualToString:myString2]?@"+":@"-")); //YES
    NSLog(@"==ity: %@", ((myString1 == myString2)?@"+":@"-")); // NO
    

    So, the compiler is likely to use isEqualToString method to process isEquals for NSString's and dereference pointers, though it had not to. And the pointers are different, as you see.

    0 讨论(0)
提交回复
热议问题