Object Copy & mutablecopy

Source: Internet
Author: User

Refer:

[1] Reference Database: programming guide for Memory Management

Assign a pointer P1 to another P2, that is, P2 = p1. In fact, P1 and P2 point to the same instance object. Any operation on P1 will affect P2, because they point to the instance objects of the same address. If you need to copy an object, instead of assigning values to pointers, you can use the copy constructor in C ++ to implement this function. Objetive-C is implemented through the copy and mutablecopy methods.

First, there are two protocols in Objetive-C: nscopying and nsmutablecopying, respectively declaring the copywithzone and mutablecopywithzone methods. Protocol: abstracts and defines methods with the same functions in several classes. Therefore, any class that requires the copy operation only needs to follow the nscopying and nsmutablecopying protocols and implement the copywithzone and mutablecopywithzone methods to copy objects.

Second, the method copy generates unchangeable objects, and the method mutablecopy generates modifiable objects. The generated modifiable object has nothing to do with whether the previously Copied object can be modified, but with the call of copy or mutablecopy.

The test code is as follows. First, locbird contains the nscopying or nsmutablecopying protocol, as shown below.

@interface LOCBird : NSObject{NSString* name_;    }@end

@implementation LOCBird- (id)init{self = [super init];if (self) {name_ = [[NSString alloc] initWithString:@"I am a Bird!!"];}return self;}- (void)dealloc{[name_release];[super dealloc];}- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];if (signature==nil) {signature = [name_ methodSignatureForSelector:aSelector];}NSUInteger argCount = [signature numberOfArguments];for (NSInteger i=0 ; i<argCount ; i++) {NSLog(@"%s" , [signature getArgumentTypeAtIndex:i]);}NSLog(@"returnType:%s ,returnLen:%d" , [signature methodReturnType] , [signature methodReturnLength]);NSLog(@"signature:%@" , signature);return signature;}- (void)forwardInvocation:(NSInvocation *)anInvocation{NSLog(@"forwardInvocation:%@" , anInvocation);SEL seletor = [anInvocation selector];if ([name_ respondsToSelector:seletor]) {[anInvocation invokeWithTarget:name_];}}@end

Create and test an object

LOCBird* bird1 =[[LOCBird alloc] init];LOCBird* bird2 = [bird1 copy];LOCBird* bird3 = [bird1 mutableCopy];NSLog(@"%@ %@",bird2 , NSStringFromClass([bird2 class]));NSLog(@"%@ %@",bird3 , NSStringFromClass([bird3 class]));

The output result is as follows:

22:17:28. 384 locmessageforward [1082: f803] @

22:17:28. 385 locmessageforward [1082: f803]:

22:17:28. 385 locmessageforward [1082: f803] ^ {_ nszone =}

22:17:28. 386 locmessageforward [1082: f803] returntype: @, returnlen: 4

22:17:28. 387 locmessageforward [1082: f803] Signature: <nsmethodsignature: 0x685ae90>

22:17:28. 388 locmessageforward [1082: f803] forwardinvocation: <nsinvocation: 0x6a45670>

22:17:28. 389 locmessageforward [1082: f803] @

22:17:28. 390 locmessageforward [1082: f803]:

22:17:28. 391 locmessageforward [1082: f803] ^ {_ nszone =}

22:17:28. 392 locmessageforward [1082: f803] returntype: @, returnlen: 4

22:17:28. 393 locmessageforward [1082: f803] Signature: <nsmethodsignature: 0x685ae90>

22:17:28. 393 locmessageforward [1082: f803] forwardinvocation: <nsinvocation: 0x68438b0>

22:17:28. 394 locmessageforward [1082: f803] I am a bird !! _ Nscfconstantstring

22:17:28. 395 locmessageforward [1082: f803] I am a bird !! _ Nscfstring

Locbird cannot respond to the copy message because it complies with the nscopying or nsmutablecopying Protocol. However, because methodsignatureforselector and forwardinvocation are overloaded, the message is forwarded to the name _ member of nsstring, therefore, the generated nsstring object is finally copied.

After modification, follow the nscopying or nsmutablecopying protocol, and the output is as follows:

22:41:09. 438 locmessageforward [1196: f803] <locbird: 0x6a6c760> locbird

22:41:09. 439 locmessageforward [1196: f803] <locbird: 0x687c880> locbird

Finally, test the nsstring and nsarray copy methods.

NSString* str1 = @"hello";NSString* str2 = @"hello";NSString* str3 = [NSString stringWithString:@"hello"];NSString* str4 = [NSString stringWithFormat:@"hello"];NSString* str5 = [[NSString alloc]initWithString:@"hello"];NSString* str6 = [[NSString alloc]initWithFormat:@"hello"];NSString* str7 = [NSString stringWithCString:"hello" encoding:NSUTF8StringEncoding];NSString* str8 = [NSString stringWithCString:"hello" encoding:NSUTF8StringEncoding];NSLog(@"%p,%p,%p,%p,%p,%p,%p,%p",str1,str2,str3,str4,str5,str6,str7,str8);NSString* cpStr1 = [str1 copy];NSMutableString* cpStr2 = [str1 mutableCopy];NSLog(@"cpStr1=%p mutCpStr1=%p",cpStr1 , cpStr2);NSArray* array = [NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6,str7,str8,nil];NSArray* imArray = [array copy];for (NSString* str in imArray) {NSLog(@"%p %@",str,str);}NSLog(@"-----------------");NSMutableArray* mutArray = [array mutableCopy];[mutArray addObject:@"hello"];for (NSString* str in mutArray) {NSLog(@"%p %@",str,str);}

The output result is as follows:

0x4640,0x4640,0x4640, 0x6a69860, 0x4640, 0x6a65a50, 0x6a6b7a0, 0x6a6aec0

22:41:06. 906 locmessageforward [1196: f803] cpstr1 = 0x4640 mutcpstr1 = 0x68695f0

22:41:06. 906 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 907 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 907 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 908 locmessageforward [1196: f803] 0x6a69860 hello

22:41:06. 908 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 909 locmessageforward [1196: f803] 0x6a65a50 hello

22:41:06. 909 locmessageforward [1196: f803] 0x6a6b7a0 hello

22:41:06. 910 locmessageforward [1196: f803] 0x6a6aec0 hello

22:41:06. 910 locmessageforward [1196: f803] -----------------

22:41:06. 911 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 911 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 912 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 912 locmessageforward [1196: f803] 0x6a69860 hello

22:41:06. 912 locmessageforward [1196: f803] 0x4640 hello

22:41:06. 913 locmessageforward [1196: f803] 0x6a65a50 hello

22:41:06. 913 locmessageforward [1196: f803] 0x6a6b7a0 hello

22:41:06. 914 locmessageforward [1196: f803] 0x6a6aec0 hello

22:41:06. 914 locmessageforward [1196: f803] 0x4640 hello

Note: Since "hello" is a constant string and is put in the constant data zone after compilation, str1 and str2 have the same pointer address, but the stringwithstring and stringwithformat do not understand why they behave differently, I don't know, but what did Apple think?

It is certain that both nsstring and nsarray are shortest copies. Only the pointer is copied, And the retaincount of the instance indicated by the pointer is added. This object is not copied.

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.