Come on! To complete the knowledge point;

Source: Internet
Author: User

The bolg:http://www.cocoachina.com/ios/20150803/12872.html of the Great God

what are the rules for @synthesize synthetic instance variables? @synthesize represents the Getter/setter method that is automatically implemented by the compiler, and does not need to be implemented manually. By default, you do not need to specify the name of an instance variable, and the compiler automatically generates an instance variable with "_" before the property name. Of course, you can also specify the name of the instance variable in the implementation code by using the @synthesize syntax.
What are the roles of @synthesize and @dynamic respectively?

Simply speaking, the @synthesize instruction tells the compiler to generate getter and setter methods during compilation. If the custom getter and setter methods overwrite the method that the compiler helped us build.
The @dynamic directive tells the compiler not to automatically generate getter and setter methods during compilation, preventing warnings from being generated during compilation. It is then possible to dynamically create bindings at run time by implementing access methods or access methods. Its main function is to use the attribute declaration of the Nsmanageobject object, because the properties of such objects are generally generated from the properties of core data, and the core data framework generates getter and setter methods for such properties when the program runs.

Atomic and Nonatomic
Atomic: The default is the property, this property is to ensure that the program in the multi-threaded case, the compiler will automatically generate some mutex lock code, to avoid the variable read and write out of sync problem, provide multithreading security.
Nonatomic: If the object does not need to consider multithreading, add this property, which will allow the compiler to generate a few mutex lock code, prohibit multi-threading, variable protection, improve performance and efficiency.
Note:
Atomic is a thread-protection technique used by OBJC, essentially preventing data errors from being read by another thread when the write is not completed. And this mechanism is very expensive system resources, so on the iphone small device, if not using the communication between multi-threaded programming, then nonatomic is a very good choice. In iOS development, the widespread use of nonatomic is also based on performance. does the weak attribute need to be nil in dealloc?
No. The system will automatically manage it for you. Note the appearance of circular references, and the processing of objects that cannot be destroyed, is good.

What is the nature of @property ?

@property = Ivar + getter + setter;

The property has two major concepts: Ivar (instance variable), access method (access methods = getter + setter).

property as an attribute of objective-c, the main function is to encapsulate the data in the object. The Objective-c object usually saves the data it needs as a variety of instance variables. Instance variables are generally accessed through the access method. Where the Get method (getter) is used to read the variable value, and the Set method (setter) is used to write the variable value. This concept has been stereotyped and becomes part of OBJECTIVE-C 2.0 through the attribute of "attributes". In the formal objective-c coding style, access methods have strict naming conventions. Because of this strict naming convention, the Objective-c language is able to automatically create an access method based on its name. You can actually think of attributes as a keyword, which means:

Arc, what are the default keywords when you do not display any of the property keywords specified?

    • The default keyword for the corresponding base data type is

Atomic,readwrite,assign

    • For a normal OC object

Atomic,readwrite,strong

What are the roles of @synthesize and @dynamic respectively?

1) There are two words in the @property, one is @synthesize and the other is @dynamic. If @synthesize and @dynamic are not written, then the default is @syntheszie var = _var;

2) The semantics of @synthesize is that if you do not manually implement setter methods and getter methods, then the compiler will automatically add these two methods for you.

3) @dynamic tell the compiler that the setter and getter methods of the properties are implemented by the user themselves and are not generated automatically. (Of course, for ReadOnly properties, only getter is required). If a property is declared as @dynamic var, then you do not provide the @setter method and the @getter method, the compile time is not a problem, but when the program runs to Instance.var = Somevar, due to the lack of setter method will cause the program to crash , or when running to Somevar = Var, the lack of getter method can also cause a crash. Compile-time is no problem, the runtime executes the corresponding method, which is called dynamic binding.

What other usage scenarios do @synthesize have after you have an auto-synthetic attribute instance variable?

Before answering this question, we need to figure out a question, under what circumstances will not autosynthesis (automatic synthesis)?

    • When the setter and getter are rewritten

    • When a getter that overrides a read-only property

    • When using the @dynamic

    • All properties defined in the @protocol

    • All properties defined in category

    • Overloaded Properties

When you overload a property in a parent class in a subclass, you must use @synthesize to manually synthesize the Ivar.

In addition to the last three, we can summarize a rule for several others: when you want to manually manage all of @property's content, you will try to implement all the "access Methods" of @property (the accessor methods) or use @dynamic for this purpose, the compiler will assume that you intend to manage @property manually, so the compiler disables autosynthesis (auto compositing).

Come on! To complete the knowledge point;

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.