【问题标题】:Difference between declaring properties in interface在接口中声明属性的区别
【发布时间】:2021-06-10 05:01:01
【问题描述】:

谁能告诉我在这样的接口中声明属性有什么区别

@interface RootViewController : UITableViewController {
  NSDate *timestamp;
}
@end

@interface RootViewController : UITableViewController 
@property (nonatomic, retain) NSDate *timestamp;
@end

@interface RootViewController : UITableViewController {
  NSDate *timestamp;
}
@property (nonatomic, retain) NSDate *timestamp;

【问题讨论】:

标签: objective-c


【解决方案1】:

前者根本不是属性。这是一个实例变量声明。

后者是一个属性,它是一个 getter/setter 对及其支持实例变量的术语。

为您合成的实例变量将以_ 为前缀。因此,如果您使用 Objective C 运行时 API 查看 RootViewController,您会发现它实际上有一个名为 _timestamp 的 ivar。

【讨论】:

  • 非常感谢分享。我稍微更新了我的代码,你能帮我吗?
  • 什么都没有真正改变。现在您只需将这两种情况结合起来。你有什么问题?
猜你喜欢
  • 2016-12-05
  • 1970-01-01
  • 2015-02-15
  • 2015-12-29
  • 2020-04-29
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多