Objective, objectivec

Source: Internet
Author: User

Objective, objectivec
I. AttributesAttribute is the syntax defined by Objective-C 2.0. It provides the default Implementation of the setter and getter methods for instance variables to simplify the program code to a certain extent, in addition, the attribute mechanism in the OC provides a convenient way to set and obtain instance variables, or the attribute provides a default configurator and accesser implementation; the methods provided by attributes are configurable. The benefits of attributes are that they are equivalent to writing this pair of methods by yourself. Attributes provide a series of clear descriptions of parameters to define the behavior of the seters and accessors, the compiler can generate corresponding methods based on the instructions you set to reduce your code volume and maintain the workload of setter and getter ), provides a channel for external operation class internal attributes. Because the attributes of the class are generally private, if this method is not used, the attributes cannot be changed by the outside world.(1) attribute DefinitionAttribute Declaration: Use @ property to declare the attribute, for example, @ property NSString * name. equivalent to two methods (setter and getter):-(void) setName :( NSString *) in @ interface *) name;-(NSString *) getName;(2) Implementation of attributesAttribute implementation: Use @ synthesize to implement attributes such as @ ynthesize name = _ name; equivalent to @ implementation to implement setter, getter-(void) setName :( NSString *) name {--}-(NSString *) getName {--} defines a complete Person class, including instance variables, attributes, initialization methods, constructor methods, and functional methods: h file is as follows: m file is as follows: main. the m file is as follows:Ii. AttributesObjective-C provides attributes to simplify the programmer's code and provide some keywords for attributes to control the implementation details of setter and getter. These keywords are called attributes) three categories of attributesCategory 1: Read-write control (readonly, readwrite, stter, getter) readonly, tells the compiler that only the getter method is declared (no setter method) for example: @ property (readonly) NSString * name; it is equivalent to-(NSString *) getName; readwrite, which tells the compiler to declare both setter and getter. For example: @ property (readwrite) NSString * name; it is equivalent to-(void) setName :( NSString *) name;-(NSString *) getName; readwrite is the default setting for read/write control.Category 2: Atomic control (nonatomic, atomic) atomic. The setter and getter methods are absolutely secure in multi-thread access, that is, the setter and getter methods implement multi-thread access processing. The default setting of atomic control is atomicnonatomic. The setter and getter methods do not implement multi-thread access processing internally. They are only used everywhere during the development of common setter and getter methods. If atomic is used, it is necessary to constantly unlock the setter and getter shackles to ensure thread access security and occupy system resources. To reduce system performance, it is usually set to nonatomic. When some attributes require thread security, is defined as atomic. For example: @ property (readwrite, nonatomic) NSString * name; equivalent to-(void) setName :( NSString *) name;-(NSString *) getName;Category 3: Assign, retain, and copy assign. Setter and getter are directly assigned values. For example, @ property (nonatomic, assign) NSString * name;-(void) setName :( NSString *) name {_ name = name ;} -(NSString *) name {return _ name;} retain. The built-in implementation of setter and getter will optimize the copy in memory. The internal implementation of setter and getter will also optimize the memory if the attribute is of a non-object type (such as int and float) attribute semantics settings use assign. If the attribute is of the object type (such as NSString and NSArray), use retain. If the attribute is an object and you want to get the copy parameter, use the copy keyword.Iii. Point syntaxPoint syntax is the syntax format defined in Objective-C 2.0. A convenient attribute access method is provided. Point syntax can be used when the usage of point syntax conforms to the system's default setter and getter writing formats. For example: [person1 setName: @ "zhangsan"]; it can be equivalent to person1.name = @ "zhangsan ";. NSString * name = [person1 name]; it can be equivalent to NSString * name = person1.name; the property is a pair of getter and setter methods, the dot syntax is another method of calling properties. Use the dot syntax to assign values: Person * person = [[Person alloc] init]; person. name = @ "zhangsan ";
Person. age = 33;
Person. gender = @ "male ";
NSLog (@ "% @, % ld, % @", person. name, person. age, person. gender );

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.