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

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

问题描述

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

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;
}

推荐答案

数字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)文件,您可以使用指定的内存语义自动生成访问器方法.大多数对象的经验法则是保留,但是NSStrings应该使用复制.而Singleton和Delegates通常应使用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.

数字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.

我涵盖了所有内容吗?

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

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