分配给属性的变量是否遵循该属性的行为?
寻找有关 Objective-C 属性在“链接”到实例变量时如何工作的一些说明。我的困惑源于如何通过 @synthesize 指令设置等于实例变量的属性,例如...
@synthesize someProp = _someIVar;
现在,如果我的 someProp 都像...
@property (retain,readonly) SomeClass* someProp
...这会...
-(id)initWithAutoreleasedInstanceOfSomeClass:(SomeClass*)thingThatIsAutoreleased {
self = [super init];
if(self) {
_someIVar = thingThatIsAutoreleased;
}
return self;
}
...导致 thingThatIsAutoreleased
被保留吗?
坦克!
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;
Now, if my someProp
is all like...
@property (retain,readonly) SomeClass* someProp
...will this...
-(id)initWithAutoreleasedInstanceOfSomeClass:(SomeClass*)thingThatIsAutoreleased {
self = [super init];
if(self) {
_someIVar = thingThatIsAutoreleased;
}
return self;
}
... cause thingThatIsAutoreleased
to be retained?
Tanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为它是只读的,所以您不会有设置器,但您可以通过设置内部成员变量来设置值。如果您设置了内部变量,那么您需要保留它。
请注意,您可以通过 KVC 调用并触发保留,
所以,回答您原来的问题,不,如果您直接设置 iVar,您将不会获得自动保留/释放。如果您正在操纵 iVar,则必须保留/释放。
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.
Note that you can call via KVC and get the retain to trigger
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.