I have added a new knowledge in iOS5, that is, ARC. In fact, I don't like it very much because I am used to managing the memory myself. However, learning is still necessary.
During iOS development, attribute definitions are often related to retain, assign, and copy. I think everyone is familiar with them. I will not introduce them here. There are many related articles on the Internet.
Now let's look at the New Keyword strong, weak, unsafe_unretained in iOS5. it can be similar to learning strong and retain corresponding to previous keywords. weak and unsafe_unretained functions are similar (a little different. We will introduce them later. These two new keywords are similar to assign ). With these new keywords in iOS5, you don't need to manually manage the memory. programmers who transfer from other languages such as java are very useful.
The strong keyword is similar to the retain keyword. When it is used, the reference count is automatically + 1, and the instance can better describe everything.
- @property (nonatomic, strong) NSString *string1;
- @property (nonatomic, strong) NSString *string2;
There are two attributes
- @synthesize string1;
- @synthesize string2;
Guess what results will the following code output?
- self.string1 = @"String 1";
- [self.string2 = self.string1;
- [self.string1 = nil;
- [NSLog(@"String 2 = %@", self.string2);
Result: String 2 = String 1
Because string2 is a property defined by strong, reference count + 1 makes them all point to @ "String 1". If you are familiar with retain, This is not difficult to understand.
Next let's look at the keyword weak:
If two attributes are declared as follows:
- @property (nonatomic, strong) NSString *string1;
- @property (nonatomic, weak) NSString *string2;
And define
- @synthesize string1;
- @synthesize string2;
What is the output below?
- self.string1 = [[NSString alloc] initWithUTF8String:"string 1"];
- elf.string2 = self.string1;
- self.string1 = nil;
- NSLog(@"String 2 = %@", self.string2);
Result: String 2 = null
For analysis, string1 is nil because self. string1 and self. string2 point to the same address and string2 does not have a retain memory address, while self. string1 = nil releases the memory. The pointer declared as weak. Once the address pointed to by the pointer is released, these pointers will be assigned as nil. This benefit effectively prevents wild pointers. In the c/c ++ development process, why do Daniel say that when the pointer space is released, the pointer should be assigned NULL. here we use the weak keyword to help us with this step.
Next let's look at unsafe_unretained.
From the name, we can see that unretained and unsafe are similar to weak because it is unretained, but it is unsafe. What is unsafe? Let's look at the instance below.
If two attributes are declared as follows:
And define
- @property (nonatomic, strong) NSString *string1;
- @property (nonatomic, unsafe_unretained) NSString *string2;
Let's guess, what will happen to the following code?
- self.string1 = [[NSString alloc] initWithUTF8String:"string 1"];
- self.string2 = self.string1;
- self.string1 = nil;
- NSLog(@"String 2 = %@", self.string2);
Please note that I have not told you to guess what output will be, because there will be no output at all, and your program will crash. What is the reason? It is actually caused by a wild pointer, so it is terrible. Why does it cause a wild pointer? Similar to the pointer declared by unsafe_unretained, string2 does not know it has been released because self. string1 = nil has released the memory, so it is a wild pointer. Then the access to the memory of the wild pointer causes crash. Therefore, use the unsafe_unretained keyword as little as possible.