Objective-c:oc Deep (copy) copy of internal mutable objects and immutable objects consider:

Source: Internet
Author: User



Internal OC: Deep (copy) copy of Mutable objects and immutable objects thinking:


Immutable objects: For example, the NSString object, because the NSString object is a constant string, you cannot change its contents, but you can modify the pointer pointing to the string. When a deep copy of the NSString object is copied, the copy is a shallow copy, but a pointer to the same object is copied, and if it is a mutablecopy copy, the system allocates a new memory space to hold the copied nsmutablestring object. , the address is new, the content is the same, they are being pointed at by different instance variable string pointers. mutable objects: For example, the Nsmutablestring object, because the Nsmutablestring object is a mutable string, so you can change its contents. When the Nsmutablestring object makes a deep copy, the system allocates a new memory space to hold the copied Nsmutablestring object, at which point the address is new and the content is the same, and they are being pointed to by a different instance variable string pointer.Summary: for non-container class objects of the system, we can assume that if copying an immutable object, copy is the pointer copy (shallow copy) and the mutablecopy is the object copy (deep copying).  if it is a copy of a mutable object, it is a deep copy, but the object returned by copy is immutable.




deep copy of immutable objects





1 // Person.h
  2 // test
  3 //
  4 // Created by ma c on 15/8/15.
  5 // Copyright (c) 2015 bjsxt. All rights reserved.
  6 //
  7
  8 #import <Foundation / Foundation.h>
  9 
10 @interface Person: NSObject <NSCopying>
11 @property (nonatomic, copy) NSString * name;
12-(id) initWithName: (NSString *) name;
13-(void) print;
14 @end
 

  1 // Person.m
  2 // test
  3 //
  4 // Created by ma c on 15/8/15.
  5 // Copyright (c) 2015 bjsxt. All rights reserved.
  6 //
  7
  8 #import "Person.h"
  9 
10 @implementation Person
11-(id) initWithName: (NSString *) name
12 {
13 self = [super init];
14 if (self)
15 {
16 _name = [name mutableCopy]; // mutableCopy
17 // _ name = [name copy]; // Immutable deep copy
18}
19 return self;
20}
21-(void) print
twenty two {
23 NSLog (@ "% @", self);
24 NSLog (@ "name:% @ ----% p", _ name, _name);
25}
26-(id) copyWithZone: (NSZone *) zone
27 {
28 return [[Person alloc] initWithName: _name];
29}
30 @end 


main function Test


1 // main.m
  2 // test
  3 //
  4 // Created by ma c on 15/8/15.
  5 // Copyright (c) 2015 bjsxt. All rights reserved.
  6 //
  7
  8 #import <Foundation / Foundation.h>
  9 #import "Person.h"
10 int main (int argc, const char * argv [])
11 {
12 @autoreleasepool
13 {
14 NSMutableString * name = [NSMutableString stringWithString: @ "Jobs"];
15 Person * p1 = [[Person alloc] initWithName: name];
16 [p1 print];
17
18
19 Person * p2 = [p1 copy];
20 [p2 print];
twenty one 
twenty two 
23 [name appendString: @ "Tom"];
24 Person * p3 = [[Person alloc] initWithName: name];
25 [p3 print];
26
27
28 Person * p4 = [p3 copy];
29 [p4 print];
30}
31 return 0;
32} 


   The test results are as follows:


 -2015-08-15 20: 35: 58.163 test [1713: 126141] <Person: 0x100206c60>
2015-08-15 20: 35: 58.164 test [1713: 126141] name: Jobs ---- 0x100206d30
2015-08-15 20: 35: 58.164 test [1713: 126141] <Person: 0x100300200>
2015-08-15 20: 35: 58.164 test [1713: 126141] name: Jobs ---- 0x100300210
2015-08-15 20: 35: 58.165 test [1713: 126141] <Person: 0x1001002d0>
2015-08-15 20: 35: 58.165 test [1713: 126141] name: JobsTom ---- 0x1001003e0
2015-08-15 20: 35: 58.165 test [1713: 126141] <Person: 0x100106980>
2015-08-15 20: 35: 58.165 test [1713: 126141] name: JobsTom ---- 0x100100250
Program ended with exit code: 0


The above result is the result of deep copy of mutableCopy: the same content after copying, but the address value of each is different, indicating that the system has opened up memory for newly created objects, which is the true deep copy.
 

2015-08-15 20: 41: 16.156 test [1724: 127122] <Person: 0x100306c40>
2015-08-15 20: 41: 16.157 test [1724: 127122] name: Jobs ---- 0x100306d10
2015-08-15 20: 41: 16.157 test [1724: 127122] <Person: 0x10010c8d0>
2015-08-15 20: 41: 16.157 test [1724: 127122] name: Jobs ---- 0x100306d10
2015-08-15 20: 41: 16.157 test [1724: 127122] <Person: 0x1002009f0>
2015-08-15 20: 41: 16.158 test [1724: 127122] name: JobsTom ---- 0x100200a00
2015-08-15 20: 41: 16.158 test [1724: 127122] <Person: 0x100200a60>
2015-08-15 20: 41: 16.158 test [1724: 127122] name: JobsTom ---- 0x100200a00
Program ended with exit code: 0

The above result is the result of deep copy of copy. You can see the same content after copying, but their address values are the same. . 




Objective-c:oc Deep (copy) copy of internal mutable objects and immutable objects consider:


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.