#pragmaMark's copy of the demo string//deep Copy--object copy--content copy (generate new Object) New Object counter +1, original object unchangedvoidstringmutablecopy () {//string = 1NSString *string= [[NSString Alloc]initwithformat:@"Age is %i",Ten]; //str = 1//a new object is created and the counter of the original object is unchangednsmutablestring *STR = [stringMutablecopy]; NSLog (@"Str:%zi",[stringRetaincount]); NSLog (@"String:%zi", [str retaincount]); //Modify the copy, the original object is unchanged[Str appendString:@"ABCD"]; NSLog (@"string:%@",string); NSLog (@"str:%@", str); //string =0[stringrelease]; [STR release];//copy also needs release}//Immutable copy immutable--Shallow copies//shallow copy--pointer copy--address copy (copy pointer, does not produce new object, return original object itself, original object counter + 1)voidstringcopy () {NSString*string= [[NSString Alloc]initwithformat:@"Age is %i",Ten]; //For the sake of performance, for the copy principle, string is inherently immutable, [string copy] does not produce a new object, but rather returns the object itself, copy rather with retainNSString *STR = [stringCopy];//produces an immutable object//NSLog (@ "%i", string = = str);//The result is that the object that 1,copy out is the sameNSLog (@"%zi",[stringRetaincount]); NSLog (@"%zi", [str retaincount]); [stringrelease]; [STR release];//use Copy or mutablecopy to release}#pragmaCopy of the mark variable string//Deep Copyvoidmutablestringcopy () {nsmutablestring*string= [Nsmutablestring stringWithFormat:@"Age is %i",Ten]; NSString*STR = [stringCopy];//produces an immutable str[str release];}//Deep Copyvoidmutablestringmutablecopy () {nsmutablestring*string= [Nsmutablestring stringWithFormat:@"Age is %i",Ten]; //will produce a new object.nsmutablestring *STR = [stringMutablecopy]; [Str appendString:@"1234"]; NSLog (@"str:%@", str); NSLog (@"string%@",string); [STR release]; }
Copy and mutablecopy of Objective-c grammar