objective c - Issues of pointing to same or different objects -


let's see example:

scenario 1:

nsstring *str1=@"hello";// str1 points literal string @"hello"

nsstring *str2=[[nsstring alloc] initwithstring:str1];

nslog(@"%p %p",str1,str2);// str1 , str2 both point same object

scenario 2:

nsstring *str3=[[nsstring alloc] initwithformat:@"hello"];

nsstring *str4=[[nsstring alloc] initwithstring:str3];

nslog(@"%p %p",str3,str4);// str3 , str4 point different objects

i want know difference between scenario 1 , 2.

it implementation detail (and indication there optimization foundation could isn't).

you should never assume 2 objects equal or unequal based on pointer equality. pointer equality useful checking if literally same object. isequal: must used check if semantically identical.

under covers, foundation knows @"hello" constant string , creating second immutable string constant string can return constant string.

it assuming initwithformat: produce non-constant string. optimization opportunity foundation could, during parsing of format string, detect no formatting done , return constant string (i'm kinda surprised doesn't-- should file bugenhancement request).


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -