1

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!

Ben Stock
  • 1,986
  • 1
  • 22
  • 30

2 Answers2

3

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.

NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
  • Well, that makes sense. The more I overthink things, the more I overlook them. I do have a follow-up question though; is KVC/KVO/binding still available without a property? – Ben Stock Feb 26 '14 at 17:34
  • According to http://www.objc.io/issue-7/key-value-coding-and-observing.html, yes you can. – NathanAldenSr Feb 26 '14 at 17:36
  • Damn, you're fast! Well played, sir. Thank you. P.S. That objc.io is amazing in terms of each article's depth. Wowza. – Ben Stock Feb 26 '14 at 17:37
2

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270