在 Objective C 中,您现在可以使用 @property 和 @synthesize 来自动生成 get 和 set 方法。我的问题是:我还需要在接口(interface)中声明属性吗?没有它,我的程序编译并运行良好。但大多数书籍和其他例子仍然有它。为什么?
@interface Person : NSObject {
// do i need the declaration "NSString name;"? why?
// i have notice that my program works fine without it.
// but many programming examples still incude it.
// NSString name;
}
@property NSString *name;
@end
@implementation Person
@synthesize name;
@end
最佳答案
这取决于运行时。现代运行时在 iOS 中使用,您不必声明 ivars。不过在 OS X 中并不总是这样。看这里 - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html
关于iphone - 如果我使用@property,还需要声明实例变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8459987/