Immutable instances, variable instances, copy and Mutablecopy

Source: Internet
Author: User

Code:

#import<Foundation/Foundation.h>//whether one instance is an immutable instance or a mutable instance//by sending a copy message to the instance, the resulting instance is an immutable instance//by sending an mutablecopy message to the instance, the resulting instance is a mutable instance//in non-ARC environments//an instance that is obtained by sending a copy or mutablecopy message to an instance requires a manual call to the release or Autorelease methodvoidStringcopy (void);voidStringmutablecopy (void);voidMutablestringcopy (void);voidMutablestringmutablecopy (void);intMainintargcConst Char*argv[])    {stringcopy ();    Stringmutablecopy ();    Mutablestringcopy ();    Mutablestringmutablecopy (); return 0;}voidStringcopy (void) {NSLog (@"nsstring Copy:"); NSString* Stringa = [[NSString alloc] Initwithformat:@"objective-c"]; //for an immutable instance, a copy message is sent to it, the returned instance remains an immutable instance, and the original immutable instance itself//This replication method is shallow copynsstring* STRINGB =stringa.copy; //value of the reference counter of the original immutable instance plus 1//The value of the reference counter for the immutable instance returned from copy message is the same as the value of the reference counter of the original immutable instance plus 1NSLog (@"Address (Stringa) =%p, Retaincount (Stringa) =%lu", Stringa, Stringa.retaincount); NSLog (@"Address (STRINGB) =%p, Retaincount (Stringa) =%lu", STRINGB, Stringb.retaincount); //the pointer to the original immutable instance is the same as the pointer to the immutable instance returned from the copy message, pointing to the same area of memoryNSLog (@"address (Stringa) = = Address (STRINGB):%@", Stringa = = STRINGB?@"YES":@"NO");    [Stringa release]; [STRINGB release];}voidStringmutablecopy (void) {NSLog (@"nsstring mutablecopy:"); NSString* Stringa = [[NSString alloc] Initwithformat:@"objective-c"]; //for an immutable instance, a mutablecopy message is sent to it, and the returned instance is a new mutable instance//the replication method is deep copynsmutablestring* STRINGB =stringa.mutablecopy; //the reference counter value of the original immutable instance does not changeNSLog (@"Address (Stringa) =%p, Retaincount (Stringa) =%lu", Stringa, Stringa.retaincount); //The reference counter for the new mutable instance returned from the mutablecopy message is 1NSLog (@"Address (STRINGB) =%p, Retaincount (Stringa) =%lu", STRINGB, Stringb.retaincount); //The pointer to the original immutable instance is different from the variable instance returned from the copy message and does not point to the same memory areaNSLog (@"address (Stringa) = = Address (STRINGB):%@", Stringa = = STRINGB?@"YES":@"NO");    [Stringa release]; [STRINGB release];}voidMutablestringcopy (void) {NSLog (@"nsmutablestring Copy:"); Nsmutablestring* Stringa = [[Nsmutablestring alloc] Initwithformat:@"objective-c"]; //for a mutable instance, a copy message is sent to it, and the returned instance is a new immutable instance//the replication method is deep copynsstring* STRINGB =stringa.copy; //The value of the reference counter for the original mutable instance does not changeNSLog (@"Address (Stringa) =%p, Retaincount (Stringa) =%lu", Stringa, Stringa.retaincount); //The reference counter for the new mutable instance returned from the mutablecopy message is 1NSLog (@"Address (STRINGB) =%p, Retaincount (Stringa) =%lu", STRINGB, Stringb.retaincount); //the pointer to the original mutable instance is different from the pointer to the immutable instance returned from the copy message and does not point to the same memory areaNSLog (@"address (Stringa) = = Address (STRINGB):%@", Stringa = = STRINGB?@"YES":@"NO");    [Stringa release]; [STRINGB release];}voidMutablestringmutablecopy (void) {NSLog (@"nsmutablestring mutablecopy:"); Nsmutablestring* Stringa = [[Nsmutablestring alloc] Initwithformat:@"objective-c"]; //for a mutable instance, a mutablecopy message is sent to it, and the returned instance is a new immutable instance//the replication method is deep copynsmutablestring* STRINGB =stringa.mutablecopy; //The value of the reference counter for the original mutable instance does not changeNSLog (@"Address (Stringa) =%p, Retaincount (Stringa) =%lu", Stringa, Stringa.retaincount); //The reference counter for the new mutable instance returned from the mutablecopy message is 1NSLog (@"Address (STRINGB) =%p, Retaincount (Stringa) =%lu", STRINGB, Stringb.retaincount); //the pointer to the original mutable instance is different from the variable instance returned from the Mutablecopy message and does not point to the same memory areaNSLog (@"address (Stringa) = = Address (STRINGB):%@", Stringa = = STRINGB?@"YES":@"NO");    [Stringa release]; [STRINGB release];}

Output:

nsstring copy:address (Stringa)=0x100106800, Retaincount (Stringa) =2Address (STRINGB)=0x100106800, Retaincount (Stringa) =2Address (Stringa)==Address (STRINGB): yesnsstring mutablecopy:address (Stringa)=0x100106800, Retaincount (Stringa) =1Address (STRINGB)=0x10010e1a0, Retaincount (Stringa) =1Address (Stringa)==Address (STRINGB): nonsmutablestring copy:address (Stringa)=0x10010e290, Retaincount (Stringa) =1Address (STRINGB)=0x10010e1e0, Retaincount (Stringa) =1Address (Stringa)==Address (STRINGB): nonsmutablestring mutablecopy:address (Stringa)=0x10010e290, Retaincount (Stringa) =1Address (STRINGB)=0x10010e2f0, Retaincount (Stringa) =1Address (Stringa)= = Address (STRINGB): NO

Immutable instances, variable instances, copy and Mutablecopy

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.