Understanding NSString comparison

后端 未结 7 1624
情歌与酒
情歌与酒 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:08

    == compares locations in memory. ptr == ptr2 if they both point to the same memory location. This happens to work with string constants because the compiler happens to use one actual string for identical string constants. It won't work if you have variables with the same contents, because they'll point to different memory locations; use isEqualToString in such a case.

提交回复
热议问题