Ben Stock
Ben Stock

Reputation: 2026

Weak or strong property declaration when returning value of related view?

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!

Upvotes: 1

Views: 116

Answers (2)

Sulthan
Sulthan

Reputation: 130082

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.

Upvotes: 2

NathanAldenSr
NathanAldenSr

Reputation: 7961

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.

Upvotes: 3

Related Questions