• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: virtual ivars
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >virtual ivars (From: Todd Heberlein <email@hidden>)

  • Prev by Date: Re: virtual ivars
  • Next by Date: Re: Drawing Text in an NSImage
  • Previous by thread: Re: virtual ivars
  • Next by thread: Copying records from to-many relationship in Core Data
  • Index(es):
    • Date
    • Thread