The strong and weak in property
Strong keyword and retain, using it, reference counting automatic +1, with an example to explain everything
@property (nonatomic, strong) NSString *string1;
@property (nonatomic, strong) NSString *string2;
There are two of these properties,
@synthesize string1;
@synthesize string2;
Guess what results The following code will output?
Self.string1 = @"String 1";
Self.string2 = self.string1;
Self.string1 = nil;
NSLog (@"String 2 =%@", self.string2);
The result is: string 2 = string 1
Since string2 is a strong-defined property, the reference count of +1 makes them point to the value @ "String 1", which is not difficult to understand if you are familiar with retain.
Then we look at the Weak keyword:
If this declares two properties:
@property (nonatomic, strong) NSString *string1;
@property (nonatomic, weak) NSString *string2;
and define
@synthesize string1;
@synthesize string2;
Let's guess, what is the output below?
Self.string1 = @"String 1";
Self.string2 = self.string1;
Self.string1 = nil;
NSLog (@"String 2 =%@", self.string2);
The result is: String 2 = null
Analysis, because Self.string1 and Self.string2 point to the same address, and string2 no retain memory address, and Self.string1=nil freed memory, so string1 is nil. A pointer that is declared as weak, the pointer to the address once released, these pointers will be assigned to nil. Such a benefit can effectively prevent the wild hands. Why does Daniel say that when the pointer space is released, the pointer will be NULL when it is developed in C + +. Here we do this step with the weak keyword.
Strong and weak