自定义访问器和“=”在属性中?

发布于 2024-12-03 04:30:24 字数 296 浏览 0 评论 0原文

你能告诉我这些自定义访问器的含义吗:

为什么要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2024-12-10 04:30:24
@property (assign,getter=isSelected) BOOL selected;

在头文件中,指定您希望其他类能够使用 myObject.isSelected 访问此属性。您在类的公共接口中定义此名称。

@synthesize title = defaultTitle;

在您的实现文件中,指定您已定义一个名为 title 的属性供其他类使用,但在您的类内部您希望实际使用名称 defaultTitle 。这通常是在您声明了自己的名为 defaultTitle 的私有实例变量(您不希望人们直接修改该变量)时完成的。

@property (assign,getter=isSelected) BOOL selected;

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.

@synthesize title = defaultTitle;

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 name defaultTitle. This is normally done when you have declared your own private instance variable named defaultTitle which you don't want people modifying directly.

活雷疯 2024-12-10 04:30:24

@property (assign,getter=isSelected) BOOL selected;

必须指定 getter 以符合命名约定。请参阅Apple的手册引用:

通常,您应该指定键值对的访问器方法名称
编码兼容(参见键值编码编程指南)——一个常见的
使用 getter 装饰器的原因是为了遵守
布尔值的 isPropertyName 约定。

@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:

Typically you should specify accessor method names that are key-value
coding compliant (see Key-Value Coding Programming Guide)—a common
reason for using the getter decorator is to adhere to the
isPropertyName convention for Boolean values.

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文