objective c - Comparing NSString -


this question has answer here:

the output of code below is,

areequal

areequal

can explain why?

nsstring *firstusername = @"nick"; nsstring *secondusername = @"nick";  if (firstusername  == secondusername) {     nslog(@"areequal"); } else {     nslog(@"arenotequal"); }  myobject *object1 = [[myobject alloc]init]; object1.identifier = @"1"; myobject *object2 = [[myobject alloc]init]; object2.identifier = @"1";  if (object2.identifier  == object1.identifier) {     nslog(@"areequal"); } else {     nslog(@"arenotequal"); } 

when explicitly type @"nick" or @"1", string literal that, efficiency, compiler storing in same memory location. if created 1 of strings in different fashion, appear @ different memory location, such if built first string concatenating 2 strings together.

generally comparing 2 strings in way not want do. want compare 2 strings equality, , want know if point same memory address. should use isequal: or isequaltostring: instead.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -