嗨,这之间有什么区别 一)
@interface SViewController : UITableViewController{
NSString *_name;
}
@property (nonatomic, retain) NSString *name;
@synthesize name = _name;
<小时/>
b)
@interface SViewController : UITableViewController{
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@synthesize name;
最佳答案
跳过 ivar,只包含属性声明。如果需要私有(private)成员和方法,请使用类扩展。
一个例子:
@interface MyObject : NSObject {
}
@property (nonatomic,retain) NSString *publicString;
-(void)publicMethod;
@end
和实现:
#import "MyObject.h"
@interface MyObject ()
@property (nonatomic,retain) NSString *internalString;
-(void)internalMethod;
@end
@implementation MyObject
@synthesize publicString;
@synthesize internalString;
-(void)publicMethod {}
-(void)internalMethod {}
@end
注意:不声明 ivar 而只拥有该属性的一个缺点是 Xcode4(<= 4.0.1)中的 gdb 在调试时不会显示该属性。编写 iOS 代码时非常烦人,因为您还不能使用 LLDB 来调试 iOS 项目。
关于iphone - 在 iPhone 中定义属性的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5406131/