问题描述
我认为我缺少有关属性属性的信息.首先,我无法理解retain
和assign
之间的区别.
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?
这如何与readwrite
或copy
一起使用?从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) 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毫无意义,因此某类型必须为id
或SomeObjectiveCClass*
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属性-保留和分配之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!