1.objective-c World of pointers
OC has always been a human feeling more perverted language, why? Because each of its variables is pointer-type, many people almost forget the existence of that *.
So often a=b the equal sign gives me the illusion that a copy of a from B is always assumed.
But this is true, but it is a pointer. A This variable contains the address where B is located.
For example, I define a student class,new stu1 and STU2, and put stu2=stu1, that when either of the two changes, the contents of the two variables will be modified.
Student *stu1=[[student Alloc]init]; [Email protected] "STU1"; Student *stu2=stu1; [Email protected] "STU2"; NSLog (@ "stu1:%@----stu2:%@", stu1.name,stu2.name);//Output two are STU2
In C + + languages, where pointers are not used as variables, of course, the two are not the same, in fact = equals is a copy.
Student stu1; Stu1.name= "STU1"; Student stu2=stu1; Stu2.name= "STU2"; cout<< "STU1:" <<stu1.name<< "STU2:" <<stu2.name<<endl;//outputs the former STU1, the latter STU2
However, the above scenario creates a so-called stack object in the C + + language, but the size of the stack is limited, and it is very similar to OC in the case of creating a heap object (pointer object).
(But the textbook teaches us to create a stack object.)
Student *stu1= new Student (); Stu1->name= "STU1"; Student *stu2=stu1; Stu2->name= "STU2"; cout<< "STU1:" <<stu1->name<< "STU2:" <<stu2->name<<endl;//output two STU2
In Java, although the creation of a variable is not preceded by a *, but in fact the default is generated by the heap object, the object to the variable assignment is just a reference.
The illusion of 2.NSString
But for NSString, we are apt to have delusions, such as I write:
NSString *[email protected] "str1"; NSString *str2=str1; [Email protected] "str2"; NSLog (@ "str1:%@----str2:%@", STR1,STR2);//outputs the former str1, the latter str2
Is the modification of Mao str2 still not affect str1? What about the hands of a good deal? This is because the [email protected] "" way to assign values, in fact, has reopened the memory, and then let STR2 point to this new place, so str2 will certainly not affect the changes str1.
It can be parsed by the address of the output pointer and the pointer to the address:
NSString *[email protected] "str1"; NSString *str2=str1; NSLog (@ "str1:%p----str2:%p", &STR1,&STR2);//pointer address, display different, this is two different pointers NSLog (@ "str1:%p----str2:%p", STR1, STR2);//content address, display the same, instructions are pointing at @ "str1" [email protected] "str2"; NSLog (@ "str1:%p----str2:%p", &STR1,&STR2);//pointer address, display different, this is two different pointers NSLog (@ "str1:%p----str2:%p", STR1, STR2);//The content address, the display is not the same, indicating that the content of str1, str2 refers to the content has changed NSLog (@ "str1:%@----str2:%@", STR1,STR2);//Output The former str1, the latter str2
The following methods, in fact, cannot be copied, as the compiler optimizes this so that several pointer variables point to the same memory area
NSString *[email protected] "Hello"; NSString *string2 = [[NSString alloc]initwithstring:string1]; NSString *string3 = [NSString stringwithstring:string1];
As can be seen, the NSString class variable follows this guideline: "Pointer A is assigned to Pointer B, the two points to the same piece of memory, and the modification points to new memory".
3. Is there a copy in the end? Shallow copy? Deep copy?
(1) NSString to nsstring copy: (the compiler did the optimization, in fact, there is no copy, with direct pointing is the same)
Even with the following copy, in fact, as in the above case, it is only the str1 point to the address copy to STR2, so that they point to the same @ "STR1"
NSString *[email protected] "AAA"; NSString *str2=[str1 copy]; NSLog (@ "str1:%p----str2:%p", &STR1,&STR2);//pointer address output, different, description pointer new NSLog (@ "str1:%p----str2:%p", STR1, STR2);//The pointer points to the content address output, which is the same, stating that the content has not been copied
Therefore, in the NSString to NSString assignment, although the same address, but once the changes will be re-pointing, so no one can interfere with each other, the following methods are equivalent, and do not necessarily like to copy, in fact, directly in the first way relatively fast.
NSString *str2=str1;
NSString *STR3 = [[NSString ALLOC]INITWITHSTRING:STR1];
NSString *STR4 = [NSString stringwithstring:str1];
NSString *STR5 = [str1 copy];
If the scene requires, for example, to allow a str1 point to be destroyed, the first copy to the new address, and then use str2 refers to the nsstring can be copied.
(2) copy of NSString to NSString:
NSString *[email protected] "AAA"; NSString *str2=[str1 mutablecopy]; NSLog (@ "str1:%p----str2:%p", &STR1,&STR2);//pointer address output, different, description pointer new NSLog (@ "str1:%p----str2:%p", STR1, STR2);//Pointer to the content address output, is not the same, indicating re-opened the memory, the contents of STR1 to the new memory, STR2 variable point to the new address
(3) Direct point of nsmutablestring to NSString:
nsmutablestring *mstr=[nsmutablestring stringwithstring:@ "abc"]; NSString *str=mstr; NSLog (@ "mstr:%p-----str:%p", mstr,str);//The pointer points to the content address, indicating that the change of MSTR will linkage str [MSTR appendstring:@ "def"]; NSLog (@ "mstr:%@-----str:%@", mstr,str),//mstr change affects str
In this case, the MSTR change will affect STR, but the STR change will be re-directed and will not affect MSTR
(4) Copy of nsmutablestring to NSString:
nsmutablestring *mstr=[nsmutablestring stringwithstring:@ "abc"]; NSString *str=[mstr copy];//nsstring *str=[mstr mutablecopy]; can also achieve
NSLog (@ "mstr:%p-----str:%p", mstr,str);
If you do not want the MSTR change to affect STR, you can copy the contents of the MSTR.
But here, [MSTR copy] and [MSTR Mutablecopy], although the original MSTR have been copied, opened up a new memory area, but the difference is that the former copy of the object is immutable, the latter is variable. Variable string assignment to NSString actually loses its dynamics.
(5) The point of nsmutablestring to nsmutablestring:
nsmutablestring *mstr=[nsmutablestring stringwithstring:@ "abc"]; Nsmutablestring *mstr2=mstr; NSLog (@ "mstr:%p-----mstr2:%p", mstr,mstr2); [Mstr appendstring:@ "def"]; NSLog (@ "mstr:%@-----mstr2:%@", MSTR,MSTR2);
This is of course the difference between the two will affect each other.
(6) Copy of nsmutablestring to nsmutablestring:
nsmutablestring *mstr=[nsmutablestring stringwithstring:@ "abc"]; Nsmutablestring *mstr2=[mstr mutablecopy]; NSLog (@ "mstr:%p-----mstr2:%p", mstr,mstr2);//The pointer content address is not the same, indicating the implementation of the copy [mStr2 appendstring:@ "def"]; NSLog (@ "mstr:%@-----mstr2:%@", mstr,mstr2);//irrelevant
In this way, a copy of the nsmutablestring is implemented, generating two mutually irrelevant mutable strings.
What happens if you copy from copy and return an immutable string to a variable mStr2?
nsmutablestring *mstr2=[mstr Copy];
Yes, it's going to blow.
Similarly, assigning a value to nsmutablestring with NSString is a dead act.
NSString *[email protected] "str1"; nsmutablestring *mstr=[str1 copy]; [MSTR appendformat:@ "abc"];
Also collapse.
To summarize:
In NSString and nsmutablestring:
The constant assignment is constant and must be mutablecopy to complete a real copy.
Variable assignment is constant, direct pointing, invariant will change with variable, copy and mutablecopy can complete copy.
Invariant assignment variable, no matter what point to copy method is dead, must be stringwithstring function initialization
Variable assignment to variable, only mutablecopy can be a real copy, copy to die.
Pointers in Objective-c objective-c, and direct pointing of objects, copy and Mutablecopy