在 Objective-C 中设置属性列表
在接触 Objective-C 编程时,我一直在努力解决的问题之一是理解如何操作属性。我可能超出了使用正确的编码语言而不是我习惯的脚本语言的舒适区,因此头文件和实现文件中的声明内容让我有些困惑。
假设我有一个字符串。我想在该字符串中添加一些文本。我应该在头文件中声明什么,在实现文件中做什么才能使其正常工作,@property 和 @synthesize 是什么?
One of the things I've been struggling with, whilst breaking into Objective-C programming, is understanding how to manipulate properties. I'm perhaps out of my comfort zone using a proper coding language as opposed to scripting languages that I'm used to, so the declaring things in header files and implementation files is confusing me somewhat.
Let's say I have a String. I wish to add some text into that string. What do I declare in the header file and what do I do in the implementation file to allow this to work properly, and what are @property and @synthesize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Objective-C 2.0 之前的糟糕日子里,为实例变量编写 getter 和 setter 是很常见的,例如
,这只是在单线程环境中。多线程应用程序还需要更多的东西。
属性为上述内容提供了简洁的简写。 @property 替换了接口中的两个声明,并为调用者提供了有关 getter 和 setter 语义的更好提示。它还允许您 @synthesize 访问器,以便编译器自动为它们生成代码(您不必 @synthesize 它们,如果需要,您可以提供自己的实现)。以上所有内容都可以替换为
这节省了相当多的输入,但您也可以从界面中看到 setFoo: 将保留其新值,并且该属性使用不安全(设置或 get)在多线程环境中(没有其他锁定机制)。
In the bad old days before Objective-C 2.0, it was common to write getters and setters for your instance variables e.g.
And that's just in the single threaded environment. There was even more stuff needed for multithreaded apps.
Properties provide a neat shorthand for the above. The @property replaces both of the declarations in the interface as well as giving the caller better hints about the semantics of the getter and setter. It also allows you to @synthesize the accessors so the compiler will generate the code for them automatically (you don't have to @synthesize them, you can provide your own implementations if you want). All of the above can be replaced by
That saves quite a lot of typing but also you can see from the interface that setFoo: will retain its new value and that the property is not safe to use (to set or get) in a multithreaded environment (without some other locking mechanism).
@property
- 声明具有访问和内存修饰符的属性。属性可以是只读或读写、非原子或原子(线程安全)、分配/保留/复制管理。实际上,你可以像我们在Tiger时代那样声明简单的getter和setter方法,但是声明@property将帮助你随时识别属性的各个方面,而无需检查实现。@synthesize - 如果您需要一个简单的属性,而无需在 getter 和 setter 中进行任何复杂的工作,则可以简化工作。它根据
@property
的定义定义了默认实现。最后,关于字符串的问题。如果您正在寻找简单的东西,例如
myObj.string += "abc"
,属性在这里不会有帮助。它不是 Objective-C 风格,无论有没有属性,您都会执行类似myObj.string = [[myObj string] stringByAppendingString:@"abc"]
或[[myObj string]appendString: @"abc"]
取决于字符串对象的可变/不可变性质。底线:在一篇文章中解释所有内容是一个相当大的话题。我建议您阅读 Apple 文档,也许还可以购买一本有关 Objective-C 的书。 Aaron Hillegass 写了一篇 - 对于任何 Objective-C 和 Mac OS X 初学者来说都是一个好的开始。
@property
- declares a property with access and memory modifiers. Properties can be readonly or readwrite, nonatomic or atomic (thread safety), assign/retain/copy managed. Actually, you can declare simple getter and setter methods like we did in the Tiger era, but declaring a@property
will help you to identify all aspects of the property at any time without checking the implementation.@synthesize - simplifies the job if you need a simple property without any complex job in getter and setter. It defines a default implementation according to the definition of
@property
.At last, your questions about string. Properties won't help here if you are looking for something simple, let's say
myObj.string += "abc"
. It's not Objective-C style and with or without property you will do something likemyObj.string = [[myObj string] stringByAppendingString:@"abc"]
or[[myObj string] appendString:@"abc"]
depending on mutable/immutable nature of the string object.As a bottom line: it's quite a big topic to explain everything in a single post. I'd recommend you to read Apple documentation and maybe purchase a book about Objective-C. Aaron Hillegass wrote one - a good start for any Objective-C and Mac OS X beginner.