分配给属性的变量是否遵循该属性的行为? [英] Do variables assigned to properties follow the behavior of that property?
问题描述
寻求对链接"到实例变量时 Objective-C 属性如何工作的一些说明.我的困惑源于如何通过 @synthesize
指令将属性设置为等于实例变量,例如...
Looking for a little clarification on how Objective-C properties work when 'linked' to instance variables. My confusion stems from how you can set a property equal to a instance variable through the @synthesize
directive, like...
@synthesize someProp = _someIVar;
现在,如果我的 someProp
就像...
Now, if my someProp
is all like...
@property (retain,readonly) SomeClass* someProp
...这会...
-(id)initWithAutoreleasedInstanceOfSomeClass:(SomeClass*)thingThatIsAutoreleased {
self = [super init];
if(self) {
_someIVar = thingThatIsAutoreleased;
}
return self;
}
... 导致 thingThatIsAutoreleased
被保留?
... cause thingThatIsAutoreleased
to be retained?
坦克!
推荐答案
因为它是只读的,所以你不会有一个 setter 但你可以通过设置内部成员变量来设置值.如果你设置了内部var,那么你需要保留它.
since it's readonly you won't have a setter but you can set the value by setting the internal member variable. If you set the internal var, then you need to retain it.
_someIVar = [thingy retain];
请注意,您可以通过 KVC 调用并触发保留
Note that you can call via KVC and get the retain to trigger
[self setValue:myValue forKey:@"someProp"];
所以,回答您最初的问题,不,如果您直接设置 iVar,您将不会获得自动保留/释放.如果您要操作 iVar,则必须保留/释放.
So, to answer your original question, No, you won't get automatic retain/release if you're setting the iVar directly. You have to retain/release if you're manipulating the iVar.
这篇关于分配给属性的变量是否遵循该属性的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!