The difference between Ios-strong and assign

Source: Internet
Author: User

OC in iOS is an object-oriented editing language, each object is an instance of the class, and in OC each object has a pointer to the object named Isa, which points to its class. All nsobject are a struct that contains the ISA pointer.

(1) Assign and weak are a weak reference;

(2) strong is strongly quoted;

Weak references and strong references in the process of referencing the most important is caused by memory changes, and memory management, in the Setter method assignment process, the value of the pointer will change, before using each instance of the pointer number is 1,,alloc,release +1, after use to free memory, The Dealloc is performed at 1, and after 1 is reduced to 0, the Dealloc is released. Examples are as follows. The specific assignment code:

(1)-Property (Nonatomic, assign) NSObject * A;

-(void) SetA: (int) a {

-A = A;

}

In the case of a weak reference, the assignment is directly, that is, the value of the original pointer is 1, the assignment does not increase the number of pointers, or 1;

(2) -Property (Nonatomic, Strong) NSObject * A;

-(void) SetA: (int) a {

[_a release];

-A = [a strong];

}

In the case of reference, after +1 is assigned, after use-1 is still 1, return to the previous state, set to nil.

and assign, after use does not carry out-1, or 1, but the point of the thing is not, always no change, and will not point to nil, so there will be chaos.

Before writing a Gobang small game, encountered this problem, assign and strong use error, encountered the system shows an unexpected error.

Example can access my GitHub address: Https://github.com/leroypus/-,ios novice, wrong can leave a message, thank you

The difference between Ios-strong and assign

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.