"Objective-c Study record" 21st Day

Source: Internet
Author: User

Warm so know new, continue to review.

In OC, the setter and getter can be omitted by declaring attributes, which makes the code concise. When using attributes, it is not necessary to declare the instance variables in the interface and M files and to implement the setter getter, which is provided automatically, but if the instance variable is customized in the interface section, the system does not generate an instance variable if it is custom-defined.

syntax: using the @property keyword, and @property is not a variable, is the method.

naming: attribute names are not underlined, only instance variables are underlined.

Property is a pair of setter and getter methods, the property type is the parameter type in the setter method, and is the return value type in the Getter method.

Characteristics:

1. Literacy (readonly,readwrite,setter =,getter =)

ReadOnly: Read-only state, notifies the compiler that properties only generate getter methods, and does not generate setter methods.

ReadWrite: Read-write state, notifies the compiler that properties both generate setter methods and also generate getter methods.

Setter =/getter =: Method Rename. Setter = Set://getter = Get:.

2. atomicity (atomic,nonatomic)

Atomic: atomicity. The default modifier guarantees that instance variables are secured in multi-threaded situations, and that the instance variable can only be accessed by one thread at a time by constantly locking the unlock implementation. However, constant locking unlocking can consume a lot of CPU performance.

Nonatomic: Non-atomic. Contrary to atomic.

3. Semantics (assign,retain,copy)

The semantic difference directly determines the internal implementation of the attribute corresponding to the Setter,getter method.

Assign: The decorated property is non-object type, and assign is the default semantic attribute.

Retain: Properties of all object types can be decorated with retain.

Copy: is also a property used to modify the object type, but it is important to ensure that the class representing the attribute type must receive nscopying and implement the Copywithzone: method. Strings are usually decorated with copy.

Semantic features involve the knowledge of memory management in OC. First on the code:

1 @synthesizeHeight =_height;2- (void) SetHeight: (cgfloat) Height3 {4_height =height;5 }6-(cgfloat) Height7 {8     return_height;9 }Ten @synthesizeSchool =_school; One- (void) Setschool: (NSString *) School A { -     if(_school! =School) -     { the [_school release]; -_school =[school retain]; -     } - } +-(NSString *) School - { +     return[[_school retain] autorelease]; A } at @synthesizeMajor =_major; -- (void) Setmajor: (NSString *) Major - { -     if(_major! =Major) -     { - [_major release]; in_major =[major copy]; -     } to } +-(NSString *) Major - { the     return[[_major retain] autorelease]; *}

You can see that the school property and the major property are not the same as the setter and getter methods that are seen in the usual way, because school and major are NSString types, object types, so they are modified with retain or copy semantics, so you need to write them, Detailed memory management knowledge is documented later.

It is also important to note that if there is no @synthensize implementation of the property, and the setter and getter methods are overridden, then the corresponding instance variable is not generated. In other words, if you rewrite the setter and getter, you must implement the property with @synthensize in order to generate the instance variable.

Dot syntax

If you have contact with other OOP languages, you should be familiar with this point syntax, the point syntax is another way to call the property, the front has been used getter to access the property, in fact, can be accessed directly through the object name. Property name.

"Objective-c Study record" 21st Day

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.