Objective-C中ivars和properties有什么区别 [英] What is the difference between ivars and properties in Objective-C

查看:24
本文介绍了Objective-C中ivars和properties有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这 3 种在 Objective-C 中使用 ivars 和属性的方式之间的语义区别是什么?

What is the semantic difference between these 3 ways of using ivars and properties in Objective-C?

1.

@class MyOtherObject; 
@interface MyObject {
}
@property (nonatomic, retain) MyOtherObject *otherObj;

2.

#import "MyOtherObject.h"
@interface MyObject {
    MyOtherObject *otherObj;
}
@property (nonatomic, retain) MyOtherObject *otherObj;

3.

#import "MyOtherObject.h"
@interface MyObject {
    MyOtherObject *otherObj;
}

推荐答案

Number 1 与其他两个不同,它提前声明了 MyOtherObject 类以最小化编译器和链接器看到的代码量,并且还可能避免循环引用.如果您这样做,请记住将 #import 放入 .m 文件中.

Number 1 differs from the other two by forward declaring the MyOtherObject class to minimize the amount of code seen by the compiler and linker and also potentially avoid circular references. If you do it this way remember to put the #import into the .m file.

通过声明一个@property(并匹配 .m 中的@synthesize)文件,您可以自动生成访问器方法,内存语义处理您指定的方式.大多数对象的经验法则是 Retain,但 NSStrings 应该使用 Copy.而单身人士和委托人通常应该使用Assign.手写访问器既乏味又容易出错,因此这可以节省大量打字和愚蠢的错误.

By declaring an @property, (and matching @synthesize in the .m) file, you auto-generate accessor methods with the memory semantics handled how you specify. The rule of thumb for most objects is Retain, but NSStrings, for instance should use Copy. Whereas Singletons and Delegates should usually use Assign. Hand-writing accessors is tedious and error-prone so this saves a lot of typing and dumb bugs.

此外,声明一个合成属性可以让您使用点符号调用访问器方法,如下所示:

Also, declaring a synthesized property lets you call an accessor method using dot notation like this:

self.otherObj = someOtherNewObject; // set it  
MyOtherObject *thingee = self.otherObj; // get it 

代替正常的消息传递方式:

Instead of the normal, message-passing way:

[self setOtherObject:someOtherNewObject]; // set it
MyOtherObject *thingee = [self otherObj]; // get it 

在幕后,您实际上是在调用如下所示的方法:

Behind the scenes you're really calling a method that looks like this:

- (void) setOtherObj:(MyOtherObject *)anOtherObject {

    if (otherObject == anOtherObject) {
        return;  
    }

    MyOtherObject *oldOtherObject = otherObject; // keep a reference to the old value for a second
    otherObject = [anOtherObject retain]; // put the new value in  
    [oldOtherObject release]; // let go of the old object
} // set it

……或者这个

- (MyOtherObject *) otherObject {  
    return otherObject;
} // get it

屁股完全痛,对吧.现在对班级中的每个 ivar 执行此操作.如果你做的不完全正确,就会出现内存泄漏.最好让编译器完成工作.

Total pain in the butt, right. Now do that for every ivar in the class. If you don't do it exactly right, you get a memory leak. Best to just let the compiler do the work.

我看到数字 1 没有 ivar.假设这不是打字错误,那很好,因为@property/@synthesize 指令也会在幕后为您声明一个 ivar.我相信这是 Mac OS X 的新功能 - Snow Leopard 和 iOS4.

I see that Number 1 doesn't have an ivar. Assuming that's not a typo, it's fine because the @property / @synthesize directives will declare an ivar for you as well, behind the scenes. I believe this is new for Mac OS X - Snow Leopard and iOS4.

Number 3 没有生成这些访问器,因此您必须自己编写它们.如果你希望你的访问器方法有副作用,你可以做标准的内存管理舞蹈,如上所示,然后在访问器方法中做任何你需要的辅助工作.如果您综合一个属性并编写自己的,那么您的版本具有优先权.

Number 3 does not have those accessors generated so you have to write them yourself. If you want your accessor methods to have side effects, you do your standard memory management dance, as shown above, then do whatever side work you need to, inside the accessor method. If you synthesize a property as well as write your own, then your version has priority.

我是否涵盖了所有内容?

Did I cover everything?

这篇关于Objective-C中ivars和properties有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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