Do variables assigned to properties follow the behavior of that property?

情到浓时终转凉″ 提交于 2019-12-13 01:29:02

问题


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!


回答1:


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

Note that you can call via KVC and get the retain to trigger

[self setValue:myValue forKey:@"someProp"];

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.



来源:https://stackoverflow.com/questions/7221864/do-variables-assigned-to-properties-follow-the-behavior-of-that-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!