实例变量与属性之间有什么区别? [英] What is the different between Instance variable vs property ?

查看:91
本文介绍了实例变量与属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多执行此操作的代码:

I've seen a lot of codes that do this:

@interface Test0 : NSObject {
        @private int iVar;
}
@property (readwrite,assign) int iVar;
@end

和其他一些代码:

@interface Test0 : NSObject {
}
@property (readwrite,assign) int iVar;
@end

我知道您使用@synthesize iVar告诉编译器为属性iVar生成getter和setter方法.

I know that you use the @synthesize iVar to tell the compiler to generate the getter and the setter methods for the property iVar.

我的问题: 我们需要声明@private int iVar;实例变量吗? 这样做的好处是什么?声明实例变量与属性的最佳实践是什么? 编译器是否将实例变量与属性链接?

My questions: do we need to declare the @private int iVar; instance variable? What the advantage of doing so? What is the best practice of declaring instance variables vs property? does the compiler link the instance variable with the property?

提前谢谢.

推荐答案

使用非易碎基类的现代Objective-C运行时和编译器(IIRC,OS X和iOS 4.0上的64位运行时以及更高的运行时)允许您省略实例变量.您的第一个示例对于较早的运行时是必需的,而后一个则是现代运行时所需的全部.

Modern Objective-C runtimes and compilers that use non-fragile base classes (IIRC, the 64-bit runtime on OS X and the iOS 4.0 and higher runtimes) allow you to omit the instance variable. Your first example is required for older runtimes, the later is all that is required in modern runtimes.

这篇关于实例变量与属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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