Objective-C属性-保留和分配之间的区别 [英] Objective-C Property - Difference Between Retain and Assign

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

问题描述

我认为我缺少有关属性属性的信息. 首先,我无法理解retainassign之间的区别.

I think I am missing something about property attributes. First, I can't understand the difference between retain and assign.

如果我使用assign,该属性是否将setter和getter的retain计数器增加1,我是否都需要对它们同时使用release?

If I use assign, does the property increase the retain counter by 1 to the setter and also to the getter, and do I need to use release to both of them?

这如何与readwritecopy一起使用?从retain计数的角度来看.

And how does this work with readwrite or copy? From the view of a retain count.

在尝试处理属性(设置者和获取者)后,何时需要使用release

I am trying to understand when i need to use release after working with a property (setter and getter)

@property (readwrite,assign) int iVar; 

assign在这里做什么?

两者之间有什么区别

@property (readwrite,assign) int iVar;

@property (readwrite,retain) int iVar;

@property (readwrite) int iVar;

非常感谢...

推荐答案

@property(readwrite,assign)之间有什么区别int iVar;到@property(读写,保留)int iVar;到@property(readwrite)int iVar;

what is the different between : @property (readwrite,assign) int iVar; to @property (readwrite,retain) int iVar; to @property (readwrite) int iVar;

@property (readwrite,assign) sometype aProperty;的设置器在语义上等效于

The setter for @property (readwrite,assign) sometype aProperty; is semantically equivalent to

-(void) setAProperty: (sometype) newValue
{
    ivar = newValue;
}

上面的内容或多或少会给您带来帮助

The above is more or less what you will get if you put

@asynthesize aProperty = ivar;

在您的实施中.

@property (readwrite,retain) sometype aProperty;的设置器在语义上等效于

The setter for @property (readwrite,retain) sometype aProperty; is semantically equivalent to

-(void) setAProperty: (sometype) newValue
{
    [newValue retain];
    [ivar release];
    ivar = newValue;
}

很显然,保留或释放int毫无意义,因此某类型必须为idSomeObjectiveCClass*

Clearly, it makes no sense to retain or release an int, so sometype must be either id or SomeObjectiveCClass*

@property (readwrite,copy) sometype aProperty;的设置器在语义上等效于

The setter for @property (readwrite,copy) sometype aProperty; is semantically equivalent to

-(void) setAProperty: (sometype) newValue
{
    sometype aCopy = [newValue copy];
    [ivar release];
    ivar = aCopy;
}

在这种情况下,不仅某种类型必须是目标C类,而且它必须响应-copyWithZone:(或等效地实现NSCopying).

In this case, not only must sometype be an objective C class but it must respond to -copyWithZone: (or equivalently, implement NSCopying).

如果省略保留,分配或复制,则默认为分配.

If you omit retain or assign or copy, the default is assign.

顺便说一句,我通过不考虑由于属性也未指定nonatomic而发生的锁定来简化了上述操作.

By the way, I have simplified the above by not considering the locking that occurs because the properties don't also specify nonatomic.

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

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