Author: After browsing some blogs, it is convenient to browse and accumulate. If there is infringement, please contact me!!

Strong and copy

The first thing to know is the difference between the two, and I think you should know the origin of setter and getter methods in OC, deep copy and shallow copy.

A.setter and getter methods:

A. Timeline:

  • In the first version of ios, we declared both attributes and underlying instance variables for exports. At the time, attributes were a new mechanism in OC and required you to declare instance variables corresponding to them
  • In a version in the middle of Xcode, it’s no longer necessary to declare instance variables for properties, because @synthesize by default accesses STR’s name, and if it doesn’t find it, it automatically generates a private variable called STR.
  • In xcode4.5 and later, the @synthesize is omitted

B. Here’s what @synthesize does:

  • One function is to have the compiler automatically generate setters and getters for you.
  • It is also useful to specify the instance variable corresponding to the attribute, for example@synthesize str = xxx;The instance variable of the operation is XXX instead of _str. If the dot m file says@synthesize str;The generated instance variable is STR; If you didn’t write at sign synthesize STR; The generated instance variable is _str. (Note that the _str instance variable does not exist). In older code, @property could only be written in@interface @endAt sign synthesize can only write at@implementation @endSince xCode 4.5 and later, @Property takes all the functionality of @Property and @synthesize.

@property (nonatomic, copy) NSString *str; This statement does three things: 1) generates declarations of getter and setter methods for _str member variables; 2) Generate implementation of setter and getter methods for _str member variables; 3) Generate a _str member variable. (Note: Member variables generated this way are private.)

C. Usage

Some grammar:

  1. On the left-hand side is the set method
  2. On the right-hand side is the get method

General method:

  1. [A setName:@””];
  2. [A name];

B. Deep copy and shallow copy

  • If the object is immutable,copyIt makes a shallow copy,mutableCopyDeep copy is made
  • If the object is mutable, then eithercopyormutableCopyThey all make deep copies
  • If it iscopy, the copied object cannot be changed
  • If it ismutableCopy, the object is mutable after being copied

The difference between:

  1. Shallow copy: Copies Pointers without opening memory addresses
  2. Deep copy: to copy Pointers and contents to create memory locations (to store copied contents)

Use:

Modify immutable objects (NSString, NSArray, NSDictionary, etc.) with copy, modify mutable objects (NSMutableString, NSMutableArray, NSMutableDictionary, etc.) with strong

C. Difference between copy and strong

NSMutableString *originStr = [NSMutableString stringWithFormat:@" Hello,originStr"]; self.strongStr = originStr; self.copyyStr = originStr; [originStr setString:@"hello,I changed"]; NSLog(@"originStr object address :%p, object pointer address :%p, object value :%@", originStr, &Originstr, originStr); NSLog(@"strongStr object address :% p, object pointer address :% p, object value :%@", _strongStr, &_strongstr, _strongStr); NSLog(@"copyyStr object address :%p, object pointer address :%p, object value :%@", _copyyStr, &_copyystr, _copyyStr); 2018-09-06 09:23:02.871307+0800 PropertyDemo[2707:246046] originStr 0x60000024F630, object pointer address: 0x7FFEE00BCA28, object value: Hello,I changed 2018-09-06 09:23:02.871465+0800 PropertyDemo[2707:246046] StrongStr object address: 0x60000024F630 object pointer address: 0x7FB9CF40ea90, object value :hello,I changed 2018-09-06 09:23:02.871570+0800 PropertyDemo[2707:246046] copyyStr object address: 0x60000024FCC0, object pointer address: 0x7FB9CF40ea98, object value: Hello,originStrCopy the code

Self. CopyyStr value: Hello,originStr is finally the result we want _copyyStr = originStr; And the self. CopyyStr = originStr; What’s the difference?

Why: When we use @property to declare a property variable, the compiler automatically generates an instance variable with an underline and property name (@synthesize copyyStr = _copyyStr), and generates its getter and setter methods. 1. Self. copyyStr = originStr, which calls setter methods for coppyStr. 2. CopyyStr setter methods are not called and there is a very critical statement in setter methods:

_copyyStr = [copyyStr copy];
Copy the code

Conclusion: When assigning to self.copyystr = originStr, the setter method of copyyStr is called, which makes a subdeep copy of the passed copyyStr and generates a new object assigned to _copyyStr, So _copyyStr no longer points to the same address or object value as originStr.

The conclusion can be drawn from the above examples:

1. When the original string is NSString, it is an immutable string, so both strong and copy are used to refer to the original object, and copy only makes a shallow copy. 2. When the source string is NSMutableString, strong simply increments the reference count of the source string by one, while copy makes a subdeep copy of the original string, resulting in a new object that points to the new object.

So, using strong only increases the reference count if the source string is NSMutableString. However, copy will perform a deep copy, resulting in unnecessary memory waste. If the original string is NSString, strong and copy are the same, and this problem is not present. However, when you declare AN NSString, you don’t want it to change, so in general, copy is recommended to avoid NSMutableString errors.

Transfer: www.jianshu.com/p/ac654c505…