问题
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