Copy and mutablecopy of Objective-c grammar

Source: Internet
Author: User

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.