Arc Declaration Property keyword in detail (strong,weak,unsafe_unretained,copy)

Source: Internet
Author: User

Arc Declaration Property keyword in detail (strong,weak,unsafe_unretained,copy)

In the development process of iOS, the definition of attributes is often related to retain, assign, copy, I think we are very familiar with this, I do not introduce, there are many related articles on the Internet.

But adding arc to iOS 5 produced several new keywords strong, weak, unsafe_unretained. We can learn from the previous keywords: strong and retain, the two new keywords weak and unsafe_unretained are similar to assign (but still somewhat different). With these new keywords in iOS5, you don't have to manage your memory manually.

(1)

strong keyword and retain, using it, reference counting automatic +1, with an example to explain everything

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

There are two properties of this

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

Guess what results 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 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.

(2)

Then we look at the weak keyword:

If this declares two properties:

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

and define

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

Let's guess, 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, 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.

(3)

Then we'll see unsafe_unretained .

From the name can be seen, unretained and unsafe, because it is unretained so and weak a bit similar, but it is unsafe, what is unsafe, see the example below.

If this declares two properties:

and define

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

Guess what happens with the code below?

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

Please note that I am not asking you to guess what the output will be because there will be no output at all, and your program will crash out. The reason is, in fact, the wild pointer caused, so the wild pointer is terrible. Why do they create wild pointers? As with the pointer declared with unsafe_unretained, the Self.string1=nil has freed the memory, but string2 does not know that it has been released, so it is a wild pointer.  Then accessing the memory of the wild pointer causes crash. So try to use less unsafe_unretained keywords.

(4)

Strong, weak, unsafe_unretained are often used when declaring attributes (declaring properties). All of the default properties are assign (base type) or strong (OC object).

You can also use these storage specifiers (storage specifiers) when declaring local variables, but you need to make a little change to the specifier. The strong equivalent embedding specifier is __strong,weak equivalent to the embedding specifier __weak,unsafe_unretained equivalent to the embedding specifier __unsafe_unretained. (Remember that these keywords start with 2 underscores) by default, all local variables are __strong variables (local variables).

__weak, __strong is used to modify variables, in addition to __unsafe_unretained, __autoreleasing are used to modify variables.
__strong is the default keyword.
__weak declares a weak reference that can be automatically nil.
__unsafe_unretained declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function, which is automatically released when the function returns.

When Arc declares a property, the default keyword for the base data type is (atomic,readwrite,assign)
When Arc declares a property, the default keyword for the normal OC object is (Atomic,readwrite,strong)

Example:

@property (nonatomic) int supportorientation; The default is assign, because it is the underlying data type and must be assign

@property (readonly) uiimage* rightimage; Default is Atomic

@property (nonatomic) bolo_baseplayercontrolview* Ctrlview; Default is strong

@property (nonatomic, weak) id<watchvideodetaildelegate> delegate; Agent use weak

(5)

LLVM official website to give some of the signals, ARC can also use retain and other keywords

------------------------------Use the utility of the following keywords in the attribute-------------------------
    • assign implies __unsafe_unretained ownership.
    • copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter.
    • retain implies __strong ownership.
    • strong implies __strong ownership.
    • unsafe_unretained implies __unsafe_unretained ownership.
    • weak implies __weak ownership.
Assign equivalentThe Unsafe_unretainedcopy function is the same as the MRC, while there are strong effects retain the difference between strongweak and unsafe_unretained is that the weak will be assigned nil with the release pointer, And unsafe_unretained will be a wild pointer.

Arc Declaration Property keyword in detail (strong,weak,unsafe_unretained,copy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.