[Turn] ARC study notes: detailed explanation of strong, weak, etc.

Many of the basics before self-study ios have been forgotten, and I reviewed it again today.

Transferred from http://mobile.51cto.com/iphone-386301.htm

A new knowledge was added in iOS5, which is ARC. In fact, I don't like it very much, because I am used to managing memory by myself. But learning is still necessary.

In the process of iOS development, the definition of attributes is 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 take a look at the new keywords strong, weak, unsafe_unretained in iOS5. You can learn strong and retain similar to the previous keywords, weak and unsafe_unretained have similar functions (a little difference, will be introduced later, these two new keywords are similar to assign is similar). With these new keywords in iOS5, you don't have to manually manage memory, which is very useful for programmers who have come from other languages ​​such as java.

The strong keyword is similar to retain. When it is used, the reference count is automatically +1, and it can be better explained by an example.

 
 
  1. @property (nonatomic, strong) NSString *string1;   
  2. @property (nonatomic, strong) NSString *string2;

have these two properties

 
 
  1. @synthesize string1;   
  2. @synthesize string2; 

Guess what the following code will output?

 
 
  1. self.string1 = @"String 1";   
  2. [self.string2 = self.string1;   
  3. [self.string1 = nil;  
  4. [NSLog(@"String 2 = %@", self.string2); 

The result is: String 2 = String 1

Since string2 is an attribute defined by strong, the reference count is +1, so that the values ​​they point to are all @"String 1". If you are familiar with retain, this is not difficult to understand.

Next, let's look at the weak keyword:

If you declare two properties like this:

 
 
  1. @property (nonatomic, strong) NSString *string1;   
  2. @property (nonatomic, weak) NSString *string2; 

and define

 
 
  1. @synthesize string1;   
  2. @synthesize string2; 

Guess again, what is the output below?

 
 
  1.     self.string1 = [[NSString alloc] initWithUTF8String:"string 1"];   
  2. elf.string2 = self.string1;   
  3. self.string1 = nil;  
  4. NSLog(@"String 2 = %@", self.string2); 

The result is: String 2 = null

Analysis, since self.string1 and self.string2 point to the same address, and string2 does not retain the memory address, and self.string1=nil releases the memory, so string1 is nil. For pointers declared as weak, once the address pointed to by the pointer is freed, these pointers will be assigned nil. This benefit can effectively prevent wild pointers. In the process of c/c++ development, why do Daniel say that after the space of the pointer is released, the pointer must be assigned to NULL. Here we use the weak keyword to help us do this step.

Then let's look at unsafe_unretained

As can be seen from the name, unretained and unsafe, because it is unretained, it is similar to weak, but it is unsafe, what is unsafe, let's see the example below.

If you declare two properties like this:

and define

 
 
  1. @property (nonatomic, strong) NSString *string1;   
  2. @property (nonatomic, unsafe_unretained) NSString *string2; 

再来猜一下,下面的代码会有什么结果?

 
 
  1. self.string1 = [[NSString alloc] initWithUTF8String:"string 1"];   
  2. self.string2 = self.string1;   
  3. self.string1 = nil;  
  4. NSLog(@"String 2 = %@", self.string2); 

请注意,在此我并没有叫你猜会有什么输出,因为根本不会有输出,你的程序会crash掉。 原因是什么,其实 就是野指针造成的,所以野指针是可怕的。为何会造成野指针呢?同于用unsafe_unretained声明的指针,由于 self.string1=nil已将内存释放掉了,但是string2并不知道已被释放了,所以是野指针。然后访问野指针的内存就造成crash.  所以尽量少用unsafe_unretained关键字。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564796&siteId=291194637