In the concept of objective-c classes and objects. The difference and linkage between member variables and attributes has never been clear. It was not until I learned that this course on the Web Objective-c the object-oriented experience that I really had a little feeling. The key conclusion is that the class uses the member variable {}, and the attribute @property is used outside the class.
Member variable member variables and their get methods.
First, let's look at the basic class member variables and their use.
// People.h
@interface People : NSObject
{
NSString *_peopleName;
}
@end
Do nothing in. m, and then call the _peoplename member variable in MAIN.M.
(as you can see, when you invoke a member variable of a class, the. Syntax notation can be used to make an error, and must be called ):
Call the class member variable "title=" >
Change to-> The result of calling P1->_peoplename is as follows:
That is, the _peoplename default is protected, and external calls need to be set to @public. Change it:
// People.h
@interface People : NSObject
{
@public
NSString *_peopleName;
}
@end
The result of calling P1->_peoplename is as follows:
2015-05-06 15:58:41.039memberAndProperty[2851:304100]p1._peopleName : (null)
Using member variables inside a class
If you want to initialize _peoplename in Init, then 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.974memberAndProperty[2895:306281]p1._peopleNamepeoplename 1
Other class-Internal methods that use this member variable are similar usages.
Set method
The above is a call to the class member variable _peoplename, if you want to attach 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);
Results:
2015-05-06 16:05:34.915 memberAndProperty[2931:309406] p1._peopleName : people name 1
2015-05-06 16:05:34.916 memberAndProperty[2931:309406] p1._peopleName : people name 2
As seen above, after you set _peoplename to @public, you can get/set operations outside of the class, calling directly or assigning values.
However, it is not recommended to use this form of p1->_peoplename. Because, it doesn't fit in with what we're saying.
"The use of member variables {} Within a class, and the use of attribute @property outside the class" is a conclusion. and setting the member variable _peoplename to @public is not safe.
Get/set method for custom member variables
It is a good choice to still _peoplename member variables by default to @protected, reading or assigning _peoplename from methods inside the class, and then indirectly passing outside the 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, Get/set these two methods are implemented in a way that invokes or assigns member variable _peoplename within a class .
// People.h
@interface People : NSObject
{
NSString *_peopleName;
}
-(NSString *)getName;
-(void)setName:(NSString *)name;
@end
Then, it is very convenient to call:
// People.h
@interface People : NSObject
{
NSString *_peopleName;
}
-(NSString *)getName;
-(void)setName:(NSString *)name;
@end
The results are as follows:
2015-05-06 16:25:36.019 memberAndProperty[3036:320317] p1.getName : people name 1
2015-05-06 16:25:36.020 memberAndProperty[3036:320317] p1.getName : people name 2
Invoking member variables from within a class is a common way of using custom Get/set. However, these two methods need to be added manually.
Property
So, is there a simpler way? Or, get/set this very common operation, can it be provided to us by default? The answer is YES!
In the new iOS SDK, you use @property to define the properties of a class, which is dedicated to calling or assigning values from outside the class.
Declare the Peoplename property first in. h:
// People.h
@interface People: NSObject
{
NSString * _peopleName;
}
@property (nonatomic, strong) NSString * peopleName;
// nonatomic non-atomic access, no synchronization mechanism, multi-threading can improve performance when not access
// strong is equivalent to a deep copy operation
@end
Use the @synthesize directive in. m to associate the Peoplename property 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.
And the underscore _ is necessary, otherwise it is not possible to correctly associate the attribute with the member variable.
Then we call directly to:
// main.m
People *p1 = [[People alloc] init];
NSLog(@"p1.peopleName : %@", p1.peopleName);
p1.peopleName = @"people name 2";
NSLog(@"p1.peopleName : %@", p1.peopleName);
The results are as follows:
2015-05-06 16:32:29.142 memberAndProperty[3094:325295] p1.peopleName : people name 1
2015-05-06 16:32:29.143 memberAndProperty[3094:325295] p1.peopleName : people name 2
In fact, the compiler is smarter than we thought, {NSString *_peoplename in the. h file;} This member variable does not need to be declared or can be. Just declare @property (nonatomic, strong) NSString *peoplename; This property, Xcode will automatically declare for us a PEOPLENAME member variable for that class , and a hidden Get/set method . Here, it shows the effect of the underline.
Conclusion
The conclusion is that the attribute @property is used outside the class using the member variable {} within the class.
Therefore, it is strongly recommended to use attribute @property outside of the class.
If you do not want to use the member variable {} Outside of the class, either set the member variable to @public or customize its Get/set method to invoke or assign a member variable from within the class using both methods. Please refer to the above for the disadvantages of these two methods and their use.
member variables and properties of classes in Objective-c