(IOS) Understanding of @ property and @ synthesize (original), iossynthesize

Source: Internet
Author: User

(IOS) Understanding of @ property and @ synthesize (original), iossynthesize

When I started to learn ios, I didn't understand some objc syntaxes, such as @ property and @ synthesize. I used to remember and use them, but I wrote more code, I have some understanding of objc and ios, and recently used MRC, So I discussed it in the Process of memory detection and variable usage, in the end, I had a certain understanding of their functions.

Generally, @ property and @ synthesize are used together. The variables declared by @ property are processed by setter and getter by default.

Property can declare various attributes of an attribute.

1.Access Method for declaring attributes:

  • Getter = getterName
  • Setter = setterName
    Declares the access attribute settings and method names.

2.Permission for declaring attribute write operations:

  • Readwrite
    Declare this attribute as a read/write attribute, that is, you can access the setting method (setter), or you can access the getter method (getter), which is mutually exclusive with readonly.
  • Readonly

Declare this attribute as a read-only attribute and only access the getter corresponding to this attribute. It is mutually exclusive with readwrite.

3.Implementation of declarative write Methods

  4.Atomicity of access methods

For more information about atomicity, check the atomicity of the operating system.

@ Synthesize defines the names of getter and setter, which are different from the variable names, so as to protect the variables from improper access.

  Function:Let the compiler automatically write a method declaration with the same name as the data member to save the declaration of the read/write method.


[Objective-C beginner's question] For @ property and @ synthesize

If you are an iOS 6.0 SDK, There is nothing strange. To declare an attribute in iOS 6.0, you only need @ property.

After @ property and @ synthesize, what does the generated setter do?

@ Property (nonatomic, copy) the code generated by setter is roughly as follows:-(void) setArrayList: (NSMutableArray *) newArrayList {NSArrayList * copy = [newArrayList copy]; [arrayList release]; arrayList = copy;} because the @ property (nonatomic, copy) attribute is declared, the copy method is called by default, while the NSMutableArray copy method returns the NSArray type, therefore, addObject cannot be called. You can only use mutableCopy. Unfortunately, @ property does not support the declaration of mutableCopy.

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.