Re: virtual ivars
Re: virtual ivars
- Subject: Re: virtual ivars
- From: Ken Thomases <email@hidden>
- Date: Sat, 29 Aug 2009 16:39:39 -0500
On Aug 29, 2009, at 3:49 PM, Todd Heberlein wrote:
I've been playing with KVC and KVO with my own setters and getters
(along with Controllers) to create virtual ivars. That is, there
never is any storage created for the variable and its value is
calculated on the fly when the getter is called.
You are confusing, or perhaps just sloppily conflating, ivars with
properties.
There's nothing "virtual" about an ivar. It either exists or doesn't.
A property is part of the interface of your class. It's defined by
the methods, not by the implementation behind those methods.
Therefore, it's very possible to have a property which is not back by
an instance variable or any other kind of storage.
This seems to have some cool potential, but it raises the following
question:
Could this break with future Xcode or OS X upgrades?
In other words, is this approach an officially supported way to code
by Apple, or do they have the freedom to break this in the future?
It's supported. It won't break. It's inherent in the definition of
properties. That is, properties are independent of any particular
implementation. So long as the interface meets the rules of KVC
compliance, the property is compliant. For KVO, there are some rules
imposed on the implementation, but none of them require that you have
an instance variable backing the property.
Among other things, consider what the methods -valueForUndefinedKey:
and -setValue:forUndefinedKey: are for.
All that said, however, you seem to think that a property requires a
setter, even if it's empty. It does not. A read-only property can be
implemented with just a getter and no setter. In most cases, if you
setter isn't going to do anything, you should just leave it out --
neither declare nor implement it.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden