Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Attribute Mechanism in Objective-C

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The property mechanism in Objective-C 2.0 provides us with a convenient way to get and set instance variables. It can also be said that properties provide us with a default setter and accessor implementation. Before we learn about properties in OC, we need to know why we implement getter and setter methods for variables. Let's take a look at the scope of the instance.

The scope of the instance variable is as follows

1. @ public: this instance variable in common can be accessed by anyone

The instance variable protected by 2.@protected can only be accessed within the class and its subclasses. The instance variable of the parent class protected is private in the subclass. The default is protected.

3.@private: this private instance variable can only be accessed within the class

Because it is impossible to use protected or private to hide the internal details of a class, it is impossible to directly access the hidden details with a class or object, so using getter and setter to access the property mechanism in the hidden instance variable OC provides us with default getter and setter methods. Next we will learn about the attribute mechanism in OC.

Definition and implementation of attribute

1. Definition of attribute

The definition of the attribute is defined by the compiler command @ property, for example, to define the getter for the private variable int an and the setter method @ property int a; with @ property to automatically add to the decorated variable

Getter and setter methods.

Demo is as follows

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

two。 Realization of attribute

In the implementation file, we use @ synthesize to implement the use of @ synthesize to modify the corresponding variables to play the role of methods like getter and setter. You can also rename a function with @ synthesize and rename a method with @ synthesize name = otherName;.

Demo is as follows

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

3. The use of attributes after definition

One thing to note when using the getter and setter methods is to take neme as an example, the getter method is called name, and the name setName; of setter is the same as normal function usage when using getter and setter methods.

1234RMB / call name's setter method [student setName: @ "ludashi"]; / / call name's getter method NSString * name = [student name]

Getter and setter methods can also be implemented with dot syntax.

12345//name 's setter method can also write student.name = @ "ludashi" / / name's getter method can also write NSString * name = student.name

The way the getter and setter methods are called above looks like they directly manipulate the properties of the class, but they are also done through methods.

Syntax related to 4.property

1. Custom access method name

The default storage method of the system is setPropertyName and the default fetch method is propertyName. You can change the names of the setter and getter methods in the following ways

1 specify the name of the getter customization with getter = getterName

Demo: @ property (getter = getterName) NSString * name

2 specify the name of the setter custom method with setter = setterName

Demo: @ property (setter = setterName) NSString * name

2. Readability and writeability of property

The readability of property determines whether an attribute has a setter method.

1readwrite: specifies that the property is readable and writable, which is the default value, so it can be omitted

@ property (readwrite) NSString * name

(2) readonly: indicates that the property is read-only. The system does not have a setter method, but there is a getter method

@ property (readonly) NSString * name

3. The semantics used in setter determine how to assign new values to data members

Strong: indicates that a strong reference relationship means ownership of an object.

Weak: indicates that the weak reference does not own the object. When the target object is destroyed, the property value is automatically set to nil.

Assign (assignment): the direct assignment counter does not add a simple assignment method. Default assignment method is suitable for basic data types.

Copy (copy) copy the original object. The counter will add one, for example, the address of the original object is 0x01, and the address of the copied object is 0x02.

Retain (reserved) passes that the address of the original object of the pointer and the assigned object is the same.

4. Atomic operation

Atomic: thread safe

Nonatomic: thread unsafe

We can abbreviate the attribute directly in the interface file, omit {} and directly use @ property to define the method to omit the @ synthesize in the implementation file, and use _ propertyName to assign a value to the attribute using self.propertyName when accessing the attribute.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report