自定义访问器和“=”在属性中?
你能告诉我这些自定义访问器的含义吗:
为什么要在 setter 旁边添加此信息:
@property (assign,getter=isSelected) BOOL selected;
关于 setter,
@property (copy,setter=setDefaultTitle:) NSString* title;
这与编写 @synthesize title=defaultTitle 相同吗?
谢谢
Can you tell me the meaning of those custom accessors :
why would you add this info next to the setter :
@property (assign,getter=isSelected) BOOL selected;
and about the setter,
@property (copy,setter=setDefaultTitle:) NSString* title;
is this the same as writing @synthesize title=defaultTitle ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在头文件中,指定您希望其他类能够使用
myObject.isSelected
访问此属性。您在类的公共接口中定义此名称。在您的实现文件中,指定您已定义一个名为
title
的属性供其他类使用,但在您的类内部您希望实际使用名称defaultTitle
。这通常是在您声明了自己的名为defaultTitle
的私有实例变量(您不希望人们直接修改该变量)时完成的。in your header file, specifies that you want to other classes to be able to use
myObject.isSelected
to access this property. You're defining this name in the public interface to your class.in your implementation file, specifies that you have defined a property named
title
for other classes to use, but internally to your class you want to actually use the namedefaultTitle
. This is normally done when you have declared your own private instance variable nameddefaultTitle
which you don't want people modifying directly.@property (assign,getter=isSelected) BOOL selected;
必须指定 getter 以符合命名约定。请参阅Apple的手册引用:
@property (copy,setter=setDefaultTitle:) NSString* title;
如果还指定 getter,效果是一样的。但是,您必须使用 @synthesize title = defaultTitle; 为您的 getter/setter 方法生成正确的方法名称。
@property (assign,getter=isSelected) BOOL selected;
must specify the getter to conform to naming convention. See Apple's manual citation:
@property (copy,setter=setDefaultTitle:) NSString* title;
It would be the same if you also specify the getter. You have to however use the
@synthesize title = defaultTitle;
to generate the proper method names for your getter/setter methods.