Objective-C magic path [8-access member variables and attributes], objective-c8-

Source: Internet
Author: User

Objective-C magic path [8-access member variables and attributes], objective-c8-

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send


Access member variables

From the perspective of object-oriented encapsulation, if you want to access member variables in the member class through methods, the member variables must have a scope qualifier (protected, public, private) before them ), these access permission modifiers will be described later.
The access to member variables is through the getter and setter methods ).

Access member attributes:

You can use the dot operator or send a message [].


The music in the above section is an example: Song. h file
@ Interface Song: NSObject {
NSString * title;
NSString * artist;
Long int duration;
}
// Operation Method
-(Void) start;
-(Void) stop;
-(Void) seek :( long int) time;
// Method for accessing member variables
-(NSString *) title;
-(Void) setTitle :( NSString *) newTitle;
-(NSString *) artist;
-(Void) setArtist :( NSString *) newArtist;
-(Long int) duration;
-(Void) setDuration :( long int) newDuration;
@ End

Song. m file
@ Implementation Song
-(Void) start {
// Start playing
}
-(Void) stop {
// Stop playing
}
-(Void) seek :( long int) time {
// Skip time
}
// Method for accessing member variables
-(NSString *) title {
Return title;
}
-(Void) setTitle :( NSString *) newTitle {
Title = newTitle;
}
-(NSString *) artist {
Return artist;
}
-(Void) setArtist :( NSString *) newArtist {
Artist = newArtist;
}
-(Long int) duration {
Return duration;
}
-(Void) setDuration :( long int) newDuration {
Duration = newDuration;
}
@ End

Summary
After encapsulation is adopted, you can access attributes through the access method. For example, [mySong title] is the content of the title member variable.
If you do not consider encapsulation, technically Objective-C can directly access member variables through objects. The access operator is "->", for example:
MySong-> title, you can also obtain the content of the title member variable.

You can also use vertex operators for custom methods, not just synthesize (attribute ).

Note:

The vertex operator and message sending are both acceptable. However, the vertex operator is generally used in attributes to set or obtain the value of instance variables.

Methods are marked as tasks in Apple documents. tasks are generally not executed by vertex operators,

Instead, the traditional square brackets form the message expression as the preferred syntax.

In addition, when synthesize is used, the attribute name should not start with new, alloc, copy, or init.

This is related to some assumptions of the compiler, because the compiler will synthesize the corresponding methods.



Attribute
For access to member variables, you must use the getter and setter methods ). In the implementation part, we also need to implement these reading methods and setting methods. In order to simplify these trivial coding Objective-C2.0, we propose the attribute concept and use the @ property keyword to define the attribute in the interface part, in the implementation section, use the @ synthesize keyword to assemble and synthesize these attributes.
This method can automatically generate the value setting method and Value Setting Method (collectively referred to as the access method ).

The procedure is as follows:

1) Use the @ property command in the interface to identify the property.

@ Interface Fraction: NSObject

@ Property int numerator, denominator;

@ End

2) In the implementation section, use the @ synthesize command.

@ Inplementation Fraction

@ Synthesize numerator, denominator;

@ End


The header file is implemented as follows: @ interface Song: NSObject {
NSString * title;
NSString * artist;
Long int duration;
}
// Operation Method
-(Void) start;
-(Void) stop;
-(Void) seek :( long int) time;
// Method for accessing member variables
@ Property (copy, readwrite) NSString * title;
@ Property (nonatomic, retain) NSString * artist;
@ Property (readonly) long int duration;
@ End

Code Description:
Syntax for declaring property: @ property (parameter) type name ;,
Here, "Parameters" are mainly divided into three categories:
• Read/write attributes (readwrite/readonly );
• Memory management (assign/retain/copy). We will introduce these memory management parameters in the memory management section;
• Atomicity (nonatomic) is related to thread security. atomicity is atomic thread security, but it affects performance. Nonatomic can be used if you are sure not to consider thread security issues. Note:

If the @ property command is used, you do not need to declare the corresponding instance variables in the implementation section.

Generally, if an attribute called x exists, the following line is included in the implementation section, causing the compiler to automatically implement a Value Method x and a setting method setX:

@ Synthesize x;

Because the generated access method is efficient and can run normally when multithreading is used on multiple machines with multiple cores.

(Here, it is thread-safe)


The. m implementation file is as follows: @ implementation Song
@ Synthesize title;
@ Synthesize artist;
@ Synthesize duration;
-(Void) start {
// Start playing
}
-(Void) stop {
// Stop playing
}
-(Void) seek :( long int) time {
// Skip time
}
@ End


Objective-Access to member functions and member variables in Class c

The method has no access level, which is similar to ObjectiveC and C (not C ++.
C defines the method, but if no declaration is given, other people cannot find it when calling it (although they can declare an identical method ). The same is true for ObjectiveC. No access protection level is available for all methods (@ property attribute is also a method)
There is only one access level, that is, the member variables defined in braces, which are public and private. It seems that there is no protected concept (maybe, but I don't need it at all ).
In ObjectiveC 2.0 or above, you rarely need to define your own member variables. The @ synthesize command can automatically generate a private member variable based on @ property.

In objective-c, what are the differences between attributes and member variables? It seems like they are used in the same way.

You can use the dot (.) syntax for properties, such as self. xxx. You can also use someClass. xxx for external calls.

The attribute is actually a simple encapsulation of a set and get methods (the get method of oc does not have a get prefix), and a private member variable (Name: underline + attribute name ).

Member variables, whether global or local, can only be used in this class or the current function, and cannot be called directly outside of it (they can be called indirectly ).

When this class is used, the property self. xxx will add a reference count to the private _ xxx variable, which is equivalent to an additional retain. If you do not use ARC, assign values to common variables and attributes, and print the reference count. If you are using ARC, you do not need to consider referencing counter-related things at all.

All in all, if your current class needs to be called externally and you need to easily access a variable, you should set this variable as an attribute. If it is used only within a class, there is no difference between using common variables and using attributes.

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.