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

Source: Internet
Author: User

Do not repost the labor results of developers


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 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-C magic path [8-access member variables and attributes]

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.