Weak or strong property declaration when returning value of related view?
-
21-12-2019 - |
Question
I read a Stack Overflow answer that compared strong
properties on an object to leashes on a dog. For every declaration made with a strong reference, a new leash is added to the dog. Once every person walking the dog goes home (or once every object with a strong reference deallocates), the dog (the allocated memory), can be freed. I think that's how it went. I apologize to the original poster if I totally butchered that. Anyway, here's my situation. I have an NSDocument
subclass that has a property called backgroundColor
. Here's what my NSDocument
subclass is doing accessor-/mutator-wise:
- (NSColor *)backgroundColor
{
return self.window.backgroundColor;
}
- (void)setBackgroundColor:(NSColor *)color
{
self.window.backgroundColor = color;
}
So, my document object doesn't actually own the "leash," but at the same time, it's important for that dog to keep walking, or the document won't have a background color. Now I think I'm just confusing myself with the metaphor. At the end of the day, I just want to know whether to declare "forwarded" properties as being strong
or weak
.
Thanks!
Solution
Don't declare it as a property. Providing the getter and setter implementations as you have is enough. A property doesn't help you here because there is no value to store.
OTHER TIPS
Since you have your own setter and getter and you don't use the property ivar, strong
or weak
doesn't have any importance. The ivar is never read or assigned.
I would probably go with strong
because [UIWindow backgroundColor]
is strong
but weak
will work exactly the same.