Member variables and attributes of classes in Objective-C

Source: Internet
Author: User

Member variables and attributes of classes in Objective-C

In the concepts of Objective-C classes and objects. the differences and relationships between member variables and attributes have not been clarified. it was not until I learned the Objective-C object-oriented experience of MOOC online course that I really felt a little bit. the most important conclusion is:Use the member variable {} in the class, and use the attribute @ property outside the class..

Member variable and Its get method.

First, let's look at the basic class member variables and their usage.

// People.h@interface People : NSObject{    NSString *_peopleName;}@end

Do not do anything in. m, and then call the _ peopleName member variable in main. m,
(It can be seen that when calling a class member variable, an error occurs when using the. Syntax symbol. RequiredUse-> to call):
Call class member variable title =>
The result of calling p1-> _ peopleName is as follows:

That is, the _ peopleName is protected by default, and the external call must be set@ Public. Change: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> // People.h@interface People : NSObject{ @public NSString *_peopleName;}@end

The result of calling p1-> _ peopleName is as follows:

2015-05-06 15:58:41.039 memberAndProperty[2851:304100] p1._peopleName : (null)
Class internal use member variables

If you want to initialize _ peopleName in init, in People. m:

// People.m- (instancetype)init{    self = [super init];    if (self) {        _peopleName = @people name 1;    }    return self;}

The result of calling p1-> _ peopleName is as follows:

2015-05-06 16:01:36.974 memberAndProperty[2895:306281] p1._peopleName : people name 1

Other internal methods of the class that use this member variable are similar.

Set Method

The above is a call to the class member Variable _ peopleName. What if I want to add a new value to it?

        // main.m        People *p1 = [[People alloc] init];        NSLog(@p1._peopleName : %@, p1->_peopleName);        p1->_peopleName = @people name 2;        NSLog(@p1._peopleName : %@, p1->_peopleName);

Result:

2015-05-06 16:05:34.915 memberAndProperty[2931:309406] p1._peopleName : people name 12015-05-06 16:05:34.916 memberAndProperty[2931:309406] p1._peopleName : people name 2

As shown above, after _ peopleName is set to @ public, you can perform the get/set operation on it outside the class and directly call or assign values.
HoweverP1-> _ peopleName is not recommendedThis form is not in line with what we call
"Use the member variable {} in the class and use the attribute @ property outside the class."Conclusion. It is not safe to set the member Variable _ peopleName to @ public.

Get/set Method for custom member variables

The member Variable _ peopleName is still @ protected by default. It is a good choice to read or assign values to _ peopleName from the internal method of the class and then indirectly pass it to the external class.
First, declare the getName and setName methods in. h:

// People.h@interface People : NSObject{    NSString *_peopleName;}-(NSString *)getName;-(void)setName:(NSString *)name;@end

In. m, the get/set methods are as follows:Call or assign a value to the member Variable _ peopleName within the class.

// People.m-(NSString *)getName {    return _peopleName;}-(void)setName:(NSString *)name {    _peopleName = name;}

Therefore, the call is very convenient:

        // main.m        People *p1 = [[People alloc] init];        NSLog(@p1.getName : %@, p1.getName);        [p1 setName:@people name 2];        NSLog(@p1.getName : %@, p1.getName);

The result is as follows:

2015-05-06 16:25:36.019 memberAndProperty[3036:320317] p1.getName : people name 12015-05-06 16:25:36.020 memberAndProperty[3036:320317] p1.getName : people name 2

Using custom get/set, Calling member variables within a class is a common method, but you need to manually add these two methods.

Attribute

Is there a simpler way? In other words, can a very common get/set operation be provided to us by default? The answer is yes!
In the new iOS SDK, @ property is used to define the attributes of a class, which is used to call or assign values to it from outside the class.
First declare the peopleName attribute in. h:

// People. h @ interface People: NSObject {NSString * _ peopleName;} @ property (nonatomic, strong) NSString * peopleName; // nonatomic non-atomic access without synchronization mechanism, multithreading is not a high-performance access. // strong is equivalent to a deep copy operation @ end.

In. m, use the @ synthesize command to associate the peopleName attribute with the _ peopleName member variable:

// People.m@implementation People@synthesize peopleName = _peopleName;- (instancetype)init{    self = [super init];    if (self) {        _peopleName = @people name 1;    }    return self;}@end

That is, when the compiler encounters @ synthesize peopleName = _ peopleName;, the get/set Method for peopleName is automatically generated.
The underline _ is essential. Otherwise, the attribute cannot be correctly associated with the member variable.
Then, we can call it directly:

        // main.m        People *p1 = [[People alloc] init];                 NSLog(@p1.peopleName : %@, p1.peopleName);        p1.peopleName = @people name 2;        NSLog(@p1.peopleName : %@, p1.peopleName);

The result is as follows:

2015-05-06 16:32:29.142 memberAndProperty[3094:325295] p1.peopleName : people name 12015-05-06 16:32:29.143 memberAndProperty[3094:325295] p1.peopleName : people name 2

In fact, the compiler is smarter than we think. the member variable {NSString * _ peopleName;} in the H file does not need to be declared. only @ property (nonatomic, strong) NSString * peopleName is declared. xcode will automatically declare a class for us by default.PeopleName member variable and the hidden get/set method. Here, the underline is displayed..

 

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.