From: http://www.jianshu.com/p/10b65df06e44

Nsstrings (or NSArray, NSDictionary) declared with @property often use the copy keyword. Why? What problems might arise if you use the strong keyword instead?

Because a parent pointer can point to a subclass object, the purpose of using copy is to keep the properties of this object unaffected by the outside world. Whether I am passed a mutable object or an immutable object, I am holding an immutable copy. If we use strong, the property may point to a mutable object, and if the mutable object is modified externally, the property will be affected.

The copy attribute expresses ownership similar to that of strong. However, the setting method does not keep the new value, but “copies” it. This property is often used to protect encapsulation when the attribute type is NSString, because the new value passed to the setting method might point to an instance of the NSMutableString class. This class is a subclass of NSString and represents a string whose value can be modified. If you do not copy the string, the value of the string may be changed without the object’s knowledge after the property is set. So make a copy of a string that is immutable, to make sure that string values in objects don’t change unintentionally. As long as the object used to implement a property is mutable, you should make a copy when setting the value of a new property.

For example:

Define an array decorated with strong:

@property (nonatomic ,readwrite, strong) NSArray *array; Then do the following:

NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
NSArray *array = @[ @1, @2, @3, @4 ];
self.array = mutableArray;
[mutableArray removeAllObjects];;
NSLog(@"%@",self.array);

[mutableArray addObjectsFromArray:array];
self.array = [mutableArray copy];
[mutableArray removeAllObjects];;
NSLog(@"%@",self.array);
Copy the code

The print result is as follows:

2015-09-27 19:10:32.523 CYLArrayCopyDmo[10681:713670] () 2015-09-27 19:10:32.524 CYLArrayCopyDmo[10681:713670] (1, 2, 3, 4)

To understand this, it’s important to know that there are two cases:

Copy and mutableCopy operations on non-collection objects; Copy and mutableCopy operations on collection objects. ####1. Copy non-collection objects:

Copy a pointer to an IMmutable object; copy a pointer to a mutableCopy object. Copy and mutableCopy are both content copies. The code is simply expressed as follows:

  • [immutableObject copy] // Shallow copy
  • [immutableObject mutableCopy] // Deep copy
  • [mutableObject copy] // Deep copy
  • [mutableObject mutableCopy] // Deep copy

For example: NSMutableString *string = [NSMutableString stringWithString:@”origin”]; //copy NSString *stringCopy = [string copy]; If you check the memory, you can find that the memory addresses of string and stringCopy are different, indicating that the content is copied or deep copied. Even if you do the following:

[string appendString:@”origion!”]

The value of stringCopy does not change, but if copy is not used, the value of stringCopy will change. And so on. So,

NSString, NSArray, and NSDictionary often use copy because they have mutable types: NSMutableString, NSMutableArray, and NSMutableDictionary, all of which may be assigned to each other, should be copied when setting the new property value to ensure that the string value in the object does not change unintentionally. ####2. Copy and mutableCopy of collection objects

Collection objects are NSArray, NSDictionary, NSSet… And so on. Copy and mutableCopy are mutable mutable objects.

NSArray *array = @[@[@”a”, @”b”], @[@”c”, @”d”]]; NSArray *copyArray = [array copy]; NSMutableArray *mCopyArray = [array mutableCopy];

If you look at the content, you can see that the addresses of copyArray and array are the same, and the addresses of mCopyArray and array are different. Note Copy copies Pointers and mutableCopy copies contents. But it is important to note that the content copy here is only copying the array object, and the elements inside the array collection are still pointer copies. This is similar to the non-collection immutable copy above. Would a mutable copy be similar? Let’s move on to a mutable copy example:

NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@”a”],@”b”,@”c”,nil]; NSArray *copyArray = [array copy]; NSMutableArray *mCopyArray = [array mutableCopy];

As expected, the memory addresses of copyArray, mCopyArray and array are different, indicating that both copyArray and mCopyArray copy the contents of the array. Similarly, we can draw the conclusion that:

Copy an immutable object, a pointer copy, and a content copy. Copy and mutableCopy are both content copies. However: copy of the content of a collection object is limited to the object itself; the object elements are still pointer copies. The code is simply expressed as follows:

  • [immutableObject copy] // Shallow copy
  • [immutableObject mutableCopy] // Single layer deep copy
  • [mutableObject copy] // Single-layer deep copy
  • [mutableObject mutableCopy] // Single layer deep copy this code conclusion is very similar to the non-collection class.