【问题标题】:Difference between @property and variable within interface declaration [duplicate]接口声明中@property和变量之间的区别[重复]
【发布时间】:2013-05-15 17:49:14
【问题描述】:

将变量放在头文件中的@interface{} 声明中与在该声明之后放置@property 有什么区别?

例如,

@interface GameCenterManager : NSObject
{
GKInvite* pendingInvite;
}
@end

相对

@interface GameCenterManager : NSObject
@property (weak, nonatomic) GKInvite* pendingInvite
@end

【问题讨论】:

标签: ios objective-c cocoa-touch cocoa


【解决方案1】:

声明一个属性会根据括号内的条件为实例变量生成 getter 和 setter。

在括号中定义变量只是将它们声明为实例变量。

以下是一些提供更多信息的链接。

http://www.cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html

Is there a difference between an "instance variable" and a "property" in Objective-c?

http://iphonedevelopment.blogspot.in/2008/12/outlets-property-vs-instance-variable.html

【讨论】:

  • 不确定,但要生成 getter 和 setter 必须综合否?
  • @PatricioIgnacioFariaValdivi 不再。只需@property 就足够了,它会自动创建一个带有下划线前缀的 iVar。您可以在接口中使用@property (nonatomic) NSString *foo 对此进行测试,无需综合即可在您的实现中获得 _foo。
  • @PatricioIgnacioFariaValdivi 您不需要从 ios6 合成,因为它是由系统自动合成的。以前的版本需要你手动合成,
  • 太棒了,很高兴知道,谢谢。但是好吧,如果他使用的是 iOS
  • @PatricioIgnacioFariaValdivi 是的! :)
猜你喜欢
  • 2015-07-29
  • 2016-12-05
  • 1970-01-01
  • 2016-04-20
  • 2011-01-10
  • 2021-09-09
  • 2014-11-21
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多