Eighth. Properties (@proporty, @synthsize), attribute of attributes, point syntax, KVC

Source: Internet
Author: User

One. Properties

1>. The. property provides the default implementation of the Setter,getter method for instance variables. With @property, @sythesize simplifies program code to some extent and enhances access security for instance variables

2>. Declaration of attributes: use @property to declare attributes (for example: @property nsstring *name;) The keyword can automatically generate a declaration of the setter and getter method for a member variable

The equivalent of two methods declared in @interface:
-(void) SetName: (NSString *) name;
-(NSString *) name;

Implementation of the property: use @synthesize to implement the property (for example: @synthesize name = _name)This keyword helps generate the implementation of the setter and getter methods for member variables
          The   equivalent of @implementation is implemented:
                    -(void) SetName: (NSString *) name;
                    -(NSString *) name;

Use of attributes Note: First, @properety and @sythesize are used in conjunction with the Declaration and use of Setter/gstter. And can be used with ",". Secondly, it can be simplified into only the @property declaration, you can complete the entire Setter/getter declaration implementation of the whole process, but note that this way generated member variables are private . (@property int age; 3 functions: 1) generate declarations of get and set methods for _age member variables; 2) generate _age member variable set and get method implementation; 3) Generate a _ Member variable of age)

3> How to rename setter and getter

Renaming declarations in. h files

form of invocation within the MAIN.M file

Example: this instance helps to review the initialization, and the use of a traversal constructor while learning properties

Teacher.h file

TEACHER.M file

MAIN.M file

Two. Properties of attributes

1. Readability: readonly, ReadWrite
1>: @property (ReadWrite,....) ValueType value;
The ReadWrite property is the default property of a variable , that is, if you (ReadWrite and readonly are not used, then your variable is the ReadWrite attribute), by adding the ReadWrite attribute your variable will have a Get method, and set methods.
2>.property (readonly,...) Valuetypevalue;
This property variable means that the variable is only readable, that is, you can only use its Get method.

MAIN.M Call

2. Type II. Atomic control (Nonatomic Atomic)

Atomic:setter,getter method is absolutely safe under multi-threaded access, that is, setter,getter internal multithreading access processing, but it needs to setter,getter locking unlock to ensure thread access security, will occupy the system resources, Reduce system performance.

Nonatomic: Typically set to Nonatomic, this is non-atomic access, without synchronization, and multithreading concurrent access improves performance. In this method, the Setter,getter method does not do multi-threaded access processing, just the normal Setter,getter method, Nonatomic is thedefault format.
Note: 1> If you do not add this property, the default is two access methods are atomic transaction access. The lock is added to the owning object instance level. So without nonatomic is safe for multithreading.

2>. (nonatomic) Non-atomic access, when assigning values to attributes without locking, multithreading concurrent access improves performance. If this attribute is not added, the default is two access methods are atomic transaction access.

Extension: Atomic is a thread-protection technique used by OBJC, basically to prevent it from being read by another thread when writing is incomplete, resulting in data errors. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.

3. Third category. Semantic settings (assign,retian,copy)

assign is used for basic data types that are non-object types (such as int,float, and so on). This is the default format. This method assigns a value directly to the Setter,getter method, without any retain operation, in order to solve the problem of the original type and the circular reference.

Extension: In an application that uses garbage collection, if you want a property to use assign, and this class conforms to the nscopying protocol, you should explicitly indicate this tag instead of simply using the default value, otherwise you will get a compile warning. This again explains to the compiler that you really need to assign a value, even if it is a copy.

the implementation of the Retain:set method is the release of the old value, retain the new value, all implementations are in this order. For OC Object Types (nsstring, Nssarray, etc.)

Extension: Specifies that retain will wake the retain message of the incoming value when the value is assigned. This property can be used only for Objective-c object types, not for core Foundation objects. (for obvious reasons, retain increases the object's reference count, and neither the base data type nor the core Foundation object has a reference count-the translator's note).

Copy: The implementation of the Set method is the old value of release, copy the new value, for NSString, block and other types., the property is the object type and you want the copy of the parameter, that is, with the Copy keyword

        extension: Setter method Copy Operation, as with the retain process, the old value is first release, then the new object is copied, and the Retaincount is 1. This is the mechanism introduced to reduce dependency on the context. It indicates that a copy of the passed-in value is used when the value is assigned. Copy work is performed by the copy method, and this property is valid only for those object types that implement the Nscopying protocol. More in-depth discussion.

Attached to the net text:
Retain vs. Copy
Copy: Create an object with an index count of 1 and then release the old object
Retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1
copy actually created a similar object, and retain is not: for example a NSString object, the address is 0x1111, the content is @ "STR" Copy to another nsstring, the address is 0x2222, the content is the same, the new object Retai N is 1, the old object does not change, and after retain to another nsstring, the address is the same (create a pointer, pointer copy), the contents of course the same, the object's retain value of +1. that is, retain is a pointer copy and copy is a copy of the content.

Example code:

Assign,retain,copy Method Instance Application (interview question)

Three. Use of point syntax

    1. Point syntax can be used for all methods that conform to the system's default setter and getter writing format
    2. For example:
      Span style= "line-height:normal;" >[personsetname: @ " small seven "; equivalent to: person. Name=@ " small seven

      NSString *name = [person name]; equivalent to:nsstring*name= person.name;

    3. A property is a getter, setter method, and point syntax is another invocation format for a property .

Note: Point syntax can be used only on the basis of declaring the setter and getter (or @property, @sythsize). No corresponding Setter/getter declaration cannot use point syntax, because point syntax is essentially just another form of invocation.

Example code:

Key understanding and common mistakes

Four. KVC

KVC (key-value-coding), a key-value encoding, is an indirect way to access an instance variable
Method. Key: The value used to identify the instance variable value: the instance variable.

core key syntax: 1>.setvalue:forkey:

2>.valueforkey:

2>: When key does not exist, the Setvalue:forundefinedkey is executed.

When this is used for network request data, sometimes there is no corresponding key value, so it is used to prevent program crashes (write-only formatting works)

Eighth. Properties (@proporty, @synthsize), attribute of attributes, point syntax, KVC

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.