The use of attributes and point syntax in Objective-c

Source: Internet
Author: User
Tags access properties

First, the attributeThe objective-c property is the syntax defined by the 2.0, which provides a setter for instance variables, a default implementation of Getter methods that simplifies program code to some extent, and enhances access security for instance variablesthe attribute mechanism in OC provides a convenient way to set up and get instance variables, or a property provides a default set of settings and accessors, and the property provides a method that is realistically configurable, the benefit of attributes: the equivalent of writing this pair of methods yourself, Properties provide a set of clearly defined parameters to define the behavior of the settings and accessors, and the compiler can generate the appropriate method for you based on the description parameters you set, reducing your code volume and maintenance effort .set (setter) and accessor (getter), which provides a channel for the internal properties of an outside Operation class. Because the properties of a class are generally private (privately) if there is no such method, the property cannot be spun by the outside world . (i), the definition of attributesDeclaration of attributes: declaring properties using @propertyFor example:@property nsstring *name; the equivalent of two methods (setter and getter) declared in @interface:-(void) SetName: (NSString *) name;-(NSString *) getName; (b), the implementation of attributesimplementation of properties: Implementing Properties Using @synthesizeFor example:@ynthesize name = _name; equivalent to @implementation implements the setter, Getter-(void) SetName: (NSString *) name{--}-(NSString *) GetName{--}For example, define a complete person class that contains instance variables, attributes, initialization methods, convenience constructor methods, and functional methods:h file as follows:  the M file is as follows:the main.m file is as follows: Properties of AttributesObjective-c provides properties that are intended to simplify programmer codeprovides some keywords for attributes to control setter, getter implementation detailsThese keywords we call Property properties (attribute)altogether 3 kinds of attribute First category : Literacy control (readonly, ReadWrite, Stter, getter)ReadOnly, tell the compiler to declare only the Getter method (no setter method)For example:@property (readonly) nsstring *name; equivalent to -(NSString *) getName;  ReadWrite, tell the compiler to declare both the setter and the GetterFor example:@property (ReadWrite) nsstring *name; equivalent to -(void) SetName: (NSString *) name; -(NSString *) getName;ReadWrite is the default setting for read-write control Type II : Atomicity control (nonatomic, Atomic)Atomic. Setter, getter method in multi-threaded access is absolutely safe, that is, setter, getter inside do multi-threaded access processing. The default setting for Atomicity control is atomicnonatomic. Setter, getter method does not do multi-threaded access processing, just ordinary setter, getter methodprogram development process, setter, getter everywhere in use, if using atomic, need to constantly unlock setter, getter shackle to ensure thread access security, will occupy system resources, reduce system performancetypically set to Nonatomic, some properties are defined as atomic when they require thread safety. For example:@property (readwrite,nonatomic) nsstring *name; equivalent to -(void) SetName: (NSString *) name; -(NSString *) getName; Class III : Semantic settings (assign, retain, copy)assign. Setter, getter internal implementation is directly assigned valueFor example:@property (nonatomic,assign) nsstring *name; -(void) SetName: (NSString *) name {_name = name;}-(NSString *) name {return _name;}retain. Setter, getter into the internal implementation will do memory optimization copy. The internal implementation of setter and getter will also do memory optimizationIf the attribute is a non-object type (such as int,float, etc.) the semantic setting of the property is used assignIf the attribute is an object type (such as NSString, Nsarray, and so on), the semantic setting of the property is used retainIf the property is an object and wants a copy of the parameter, use the Copy keyword  Third, point grammarThe point syntax is the syntax format defined in OBJECTIVE-C 2.0. Provides a convenient way to access propertiesuse of point syntaxpoint syntax can be used to match the system default setter, Getter writing format methodFor example:[Person1 setname:@ "Zhangsan"], can be equivalent to write person1.name = @ "Zhangsan";.   NSString *name = [person1 name]; Can be written in equivalentnsstring *name = person1.name;property is a pair of getter, setter methods, and point syntax is another invocation format for a propertyuse point syntax to assign values:Person *person = [[Person alloc] init]; person.name = @ "Zhangsan";
person.age =;
Person.gender = @ "male";
NSLog (@ "%@,%ld,%@", Person.name,person.age,person.gender);

   

The use of attributes and point syntax in Objective-c

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.