Attribute mechanism in objective-C

Source: Internet
Author: User
Tags custom name

The attribute mechanism in objective-C 2.0 provides us with a convenient way to obtain and set instance variables. It can also be said that the attribute provides us with a default configurator and accesser implementation. Before learning the attributes in OC, we need to know why to implement the getter and setter methods for the variables. Let's take a look at the scope of the instance.

The scope of instance variables is as follows:

1. @ public: all variables of the instance can be accessed;

[Email protected]: protected. This instance variable can only be accessed within this class and its subclass. The instance variable of the parent protected class is private in the subclass. The default value is protected;

[Email protected]: private. This instance variable can only be accessed within this class;

 

Because protected or private is used to hide the internal details of a class, and classes or objects cannot directly access the hidden details, getter and setter are used to access the hidden instance variables, the attribute mechanism in OC provides the default getter and setter methods. Next we will learn about the attribute mechanism in OC.

 

Attribute definition and implementation

1. Attribute Definition

The attribute definition is defined by the compiler command @ property, for example, the private variable int A; the getter and setter methods are defined, @ property int; @ property automatically adds the modified variable

Getter and setter methods.

The demo is as follows:

1234567891011 @interface Student : NSObject{    NSString *name;    int idNumber;}// Define attributes@property NSString *name;@property int idNumber; // Define other methods@end

 

2. Attribute implementation

In the implementation file, we use @ synthesize to implement it. Modifying the corresponding variable with @ synthesize serves the implementation of methods like getter and setter. You can change the name of the function by @ synthesize name = othername.

The demo is as follows:

 

1234 @implementation Student @synthesize name, idNumber;    // Implementation of other methods@end

 

? ? 3. Use of defined attributes

? ? Note the following when using the getter and setter Methods: Take Neme as an example. The getter method is named name, while the setter method is named setname. The getter and setter methods are used in the same way as normal functions.

?

1234 // Call the setter method of name[student setName : @"ludashi"];// Call the getter method of nameNSString *name = [student name];

? ? The getter and setter methods can also be implemented using the dot syntax.

?

12345 // The setter method of name can also be written in this waystudent.name  =  @“ludashi”; // The getter method of name can also be written in this wayNSString *name = student.name;

? ? The above call of the getter and setter methods seems to be a direct operation of class attributes, in fact, they are also operated through methods.

 

? 4. property-related syntax

? ? 1. Custom access method name

? ? ? The default storage method is setpropertyname, and the default storage method is propertyname. You can change the names of the setter and getter methods in the following ways.

? ? ? (1) Use getter = gettername to specify the custom name of getter.

? ? ? ? ? Demo: @ property (getter = gettername) nsstring * Name;

 

? ? ? (2) Use setter = settername to specify the name of the setter custom Method

? ? ? ? ? Demo: @ property (setter = settername) nsstring * Name;

 

? ? 2. read/write Property

? ? ? Whether the property has a setter method depends on its read/write ability.

? ? ? (1) readwrite: specifies that the attribute can be read and written. This is the default value and can be omitted.

? ? ? ? ? @ Property (readwrite) nsstring * Name;

 

? ? ? (2) readonly: indicates that the attribute is read-only. The system does not use the setter method, but the getter method is available.

? ? ? ? ? @ Property (readonly) nsstring * Name;

 

? 3. Semantics used in setter: determines how to assign a new value to a data member.

? ? ? Assign

? ? ? Copy: copy the original object. The counter will add one. For example, the original object address is 0x01, and the Copied object address is 0x02;

? ? ? Retain (retained): in this way, the pointer is passed. The original object and the address of the assigned object are the same.

? ?

? 4. Atomic operations

? ? ? ? Atomic: thread-safe;

? ? ? ? Nonatomic: The thread is insecure;

 

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.