All properties of @property in Objective-c

Source: Internet
Author: User

1,assign:

Simple assignment without changing the index count

Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The result is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.

Application:

For underlying data types (such as nsinteger,cgfloat) and C data types (int, float, double, char, and so on)

For simple data types

2,retain:

Relative to strong, the reference count is used, retain+1,release-1; When the reference count is 0 o'clock, Dealloc is called and memory is freed

3,copy:

For non-shared memory, each pointer has its own memory space

4,atomic//Default Properties

A, when a variable is declared as atomic, it means that only one thread can access it in multiple threads

B, when a variable is declared as atomic, the variable is thread-safe, but it affects the speed of access,

C, when a variable is declared as atomic, the access lock needs to be set in a non-ARC compilation environment to ensure that the variable is properly get/set

5,nonatomic

A, when a variable is declared as nonatomic, it means that multiple threads can access it at the same time

B, when a variable is declared as nonatomic, it is non-thread-safe, access speed is fast;

C, when a variable is declared as Nonatomic, it is easy to get out of control when two different threads are accessing it.

Summary: Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:?


if (Property! = newvalue) {

[Property release];

property = [NewValue retain];

}

Default property in 6.strong://arc, equal to retain in non-arc

Corresponds to the Retain,

Application Scenarios:

The strong property is used in Arc

@property (strong,nonatomic) Viewcontroller *viewcontroller;

7,weak:

Corresponds to the Assign,

Application Scenarios:

Used for iboutlets, such as Uiviewcontroller subclasses, which are general controls.

@property (Weak, nonatomic) Iboutlet UIButton *mybutton;

Examples of differences between strong and weak:

Premise:

We compare the object of strong or weak to a kite, the kite wants to break free from the bondage of the line, fly freely, if there is a line at this time, then this kite will not break

Process Analysis

Variables for the Strong property:

When we declare a variable that points to a kite as strong, you have a line that controls the kite, and if there are five people at the same time controlling the kite (that is, the kite object has three strong-type variables pointing to it), then there is only one case, The kite will be free from the shackles of the line: these three people have to let go of the line, (release)

Variables for the Weak property:

When we declare a variable pointing to a kite as weak, at this time, like the spectators standing next to the kite, when the top three people still hold the line of the hand, they can only see the kite, and can not control it, they can only point to the kite with the finger, and shouted, "See, the kite flies really high!" ", however, when the top three people put their hands off the line, at this time, the kite flew away, see, no matter how many viewers, they can no longer see the kite, this story tells us a truth: when the strong type of pointer is released, all the weak pointers to the same object are zeroed out.

8,readonly

Only get method, no set method

9,readwrite//Default Properties

There is a Get/set method

10,unsafe_unretauined

Used in the ARC compilation environment, in this context, similar to assign. It just tells Arc how to correctly invoke retain and release declared as unsafe_unretauined variable


@property all the properties in Objective-c

Related Article

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.