财产声明和自动备份存储分配 [英] Property Declaration and Automatic Backing Storage Allocation

查看:92
本文介绍了财产声明和自动备份存储分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解Objective-C属性,并且我对它们的语法有一些挥之不去的问题。

I'm trying to understand Objective-C properties and I have some lingering questions about their syntax.

明确声明属性的ivar有什么区别像这样:

What is the difference between explicitly declaring an ivar for a property like this:

@interface myObject1 : NSObject {
  NSString *title;
}
@property (copy) NSString *title;
@end

这个:

@interface myObject2 : NSObject {
}
@property (copy) NSString *title;
@end

myObject2示例似乎有效。是否可以在myObject2中实现属性,或者是否应始终明确定义关联的ivar?

The myObject2 example seems to work. Is it OK to implement properties as in myObject2 or should the associated ivar always be explicitly defined?

未明确声明ivar有什么后果?

What are the ramifications of not explicitly declaring the ivar?

推荐答案

在现代Objective C运行时(nonfragile-abi)它们是相同的,ivar支持将由@synthesize声明自动创建。这是iPhone和64位Mac OS X应用程序使用的运行时。 32位Mac OS X使用遗留运行时,无法合成ivar,而您编写的第二位代码无法正确编译。

On the modern Objective C runtime (nonfragile-abi) they are the same, the ivar backing will be created automatically by the @synthesize declaration. This is the runtime used by iPhone, and 64 bit Mac OS X apps. 32 bit Mac OS X use the legacy runtime, where it is not possible to synthesize the ivar, and the second bit of code you wrote would not compile properly.

最多最新版本的iPhone模拟器使用现代运行时,但较旧的版本不使用。因此,虽然两个代码示例都适用于实际的iPhone(合成必要的存储),但第二个示例将无法为模拟器编译,除非您拥有最新的Xcode。

The most recent versions of the iPhone simulator use the modern runtime, but older ones don't. So while both code examples will work on actually iPhones (synthesizing the necessary storage), the second example will fail to compile for the simulator unless you have an up to date Xcode.

这篇关于财产声明和自动备份存储分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆