iphone - 如果我使用@property,还需要声明实例变量吗?

标签 iphone objective-c ipad ios5 properties

在 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/

相关文章:

iphone - 如何向 UISlider 控件添加增量点击调整?

javascript - iOS 中的 Exif 方向问题

ios - 如何使 View Controller 转到随机 View Controller

ios - 如何将 ios 3.0 项目升级到 ios 6.0 onwords

iphone - ios SDK命令行工具

iphone - 转换后的 MP3 格式不支持 11025 和 22050 采样率

iphone - 是否可以使用其他应用程序(例如 Skype)打开 iPhone 电话号码链接或更改默认拨号器?

objective-c - 如何将图像编码为字符串格式?

objective-c - 通过索引访问 NSMutableDictionary 中的对象

ios - 不要在水平紧凑的环境中折叠 UISplitViewController