Article source: www.cocoachina.com/ios/2015051…

When we declare an NSString property, we usually have two choices (based on ARC environments) for its memory-related features: strong and copy. So what’s the difference between the two? When to use strong and when to use copy? Let’s look at an example first. \

The sample

We define a class and declare two string attributes for it, as follows:

1234 @interface TestStringClass ()``@property (nonatomic, strong) NSString *strongString; ``@property (nonatomic, copy) NSString *copyedString; ``@end

The above code declares two string properties, one of which is a memory property of strong and one of which is copy. Let’s look at the differences.

First, we assign the two properties to an immutable string,

12345678 - (void)test {``     NSString *string = [NSString stringWithFormat:@ "abc" ]; ` ` self.strongString = string; ` ` self.copyedString = string; ` ` NSLog(@ "origin string: %p, %p" , string, &string); ` ` NSLog(@ "strong string: %p, %p" , _strongString, &_strongString); ` ` NSLog(@ "copy string: %p, %p" , _copyedString, &_copyedString); ` `}

The output is:

123 origin string: 0x7fe441592e20, 0x7fff57519a48``strong string: 0x7fe441592e20, 0x7fe44159e1f8``copy string: 0x7fe441592e20, 0x7fe44159e200

In this case, both strong and copy objects point to the same address that string points to. If we were to print the reference count of a string in an MRC environment, we would see that the reference count is 3, meaning that both strong and copy add 1 to the reference count of the original string object.

Next, let’s change string from immutable to mutable and see what happens. Here’s the next sentence

1 NSString *string = [NSString stringWithFormat:@ "abc" ];

To:

1 NSMutableString *string = [NSMutableString stringWithFormat:@ "abc" ];

The output is:

123 origin string: 0x7ff5f2e33c90, 0x7fff59937a48``strong string: 0x7ff5f2e33c90, 0x7ff5f2e2aec8``copy string: 0x7ff5f2e2aee0, 0x7ff5f2e2aed0

As you can see, the copy property string is no longer pointing to the string object. Instead, it is making a deep copy of the string and making the _copyedString object point to the string. In the MRC environment, print the reference count for both, and you can see that the reference count for string is 2 and the reference count for _copyedString is 1.

If we modify the string, we can see: Since _strongString points to the same object as string, the value of _strongString will change with it (note that the type of _strongString is actually NSMutableString, not NSString); And _copyedString points to another object, so it doesn’t change.

conclusion

Since NSMutableString is a subclass of NSString, an NSString pointer can point to an NSMutableString object, and it’s OK to have our strongString pointer point to a mutable string.

As the above example shows, when the source string is NSString, the string is immutable, so both strong and copy objects refer to the source object, and the copy operation is only a shallow copy.

When the source string is NSMutableString, the strong attribute simply increases the reference count of the source string, while the copy attribute makes a subdeep copy of the source string, producing a new object that the copy attribute object points to. Also note that the copy property object is always of type NSString, not NSMutableString, so it is immutable.

There is also a performance issue here, where the source string is NSMutableString, strong simply increases the reference count of the object, and copy performs a deep copy, so there is a difference in performance. If the source string is NSString, there is no problem.

Therefore, when declaring the NSString attribute, whether to choose strong or copy depends on the actual situation. However, when we declare an object as an NSString, we generally don’t want it to change, so in most cases we recommend copy to avoid unexpected problems caused by mutable string changes.

There are some interesting things about string memory management that you can learn from the NSString feature analysis.

reference

  1. NSString copy not copying?
  2. NSString feature analysis learning
  3. When do YOU use copy for NSString and when do you use strong