Introduction to objective-c @ property commands

Source: Internet
Author: User

Recently I learned objective-C. For Beginners, is it not a habit to add things in it? Is it strange to @ property @ synthesize? After all, there is no such "mess" in C !!

First, let's introduce this command @ property.

 

This "mess" is actually a good thing. If you use a very C Specification to write a class, do you want to write and read data to members of this class? The answer is inevitable, so you have to write a-(void) setx (ID ..) and a-(ID) x (x represents the name of the member) to write and read the data of this Member, right? So, the objective-c2.0 adds this "mess" to help you get the code that needs to be repeated over and over again in one breath (although you can copy it, but I can ensure that, you will press command + C command + V to obtain cramps ).

 

Well, there's not much nonsense. It's useful.

 

 

 

 

@ Property: There are three pairs of parameters in the command. Someone may ask you, the synthesis method is the synthesis method. What are so many parameters? Don't worry. You will be able to get started later.

 

1, readwrite, readonly, set the available access level

2, assign/retain/copy
The assign and setter Methods assign values directly without any retain operations.
The retain and setter Methods perform the release old value on the parameter and then retain the new value.
The copy and setter Methods perform the copy operation. Like the retain processing process, the old value is release first, and then the new object is copied. retaincount is 1. This is a mechanism introduced to reduce context dependencies.

3, nonatomic/atomic.

 

The first group of parameters will not be introduced. I believe everyone understands this. I want to focus on the second set of parameters. I wrote a simple description above, which may be a bit vague. It doesn't matter. Let's talk about it in detail below.

 

Assume that you allocated a piece of memory with malloc and assigned its address to pointer A. Then you want pointer B to share the block of memory, so you assign a value to assign) b. At this time, a and B point to the same memory. Can a directly release this memory when a no longer needs it? The answer is no, because a does not know whether B is still using this memory. If a is released, B will cause crash when using this memory. (You can think about this first)

 

Assign is a simple assignment that does not change retaincount. When you set native types such as int, Char, and float, using this parameter will have a very good effect (because it is the same basic type, a = 1 (assign) a B is of course also 1), !! When you set an image like a few objects, there will be an obvious problem (because we all use pointers ). I think you have understood it. Suppose A points to the 0x1111 address and opens up a piece of memory and B points to this address (apparently assign). What about release a B? Is B suffering? What should I do? In objective-C, a reference counting mechanism is added, and you need to use retain. In the same case, if B = [A retain]; [a release]; what is the difference? In this case, the reference counting mechanism in objective-C takes effect. At the beginning, a applied for a space. At this time, retaincount = 1; B = [A retain]; (retaincount = 2 ), [A release] (Note: At this time, the memory space does not release retaincount = 1 and will not be released until 0 is 0). Therefore, B can still rest assured that it will be released. Now you understand it!

There is another copy, which is easy to understand. After a points to the 0x1111 address B copy, it does not point to the 0x1111 address, but may point to the 0x2222 address, in this way, B has nothing to do with a, and they do not make water in the river. If you want to say that they are related because they are equal, I can't do anything, it's just like someone said that you have a one hundred million dollar bill, because he is also a one hundred-dollar bill !!!!

 

Now, I have finally reached the end. If there is another nonatomic/Atomic Pair, I do not know what atomic operations are or what non-atomic operations are. Please give me more advice !!!

 

Also, pay attention to the default parameter of @ property, so I will not mention it. You can check it by yourself...

 

What may be wrong? Please criticize and correct me. I am also a beginner. I am so hard at this class! Bless the Buddha!

 

 

 

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.