#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString * name;
@end
这样就直接能使用p.name了。
好像不需要如下定义:
#import <Foundation/Foundation.h>
@interface Person : NSObject{
NSString* _name;
}
@property NSString * name;
@end
这两种方式有啥区别呢?
From my experience, it is recommended to use property to define all names, so that the variable memory operation method can be clearly distinguished (multi-threading is too rare and can be ignored). And, for me, this is one of the easier ways to identify global variables. Using _name directly in the m file can quickly understand that this is a global variable. I think it is a very good habit to use properties as global variables. In addition, the synthesissize mentioned by @freewolf here can actually be omitted. Here is a simple setter as an example.