On 20 Dec 2008, at 2:28 pm, Ken Thomases wrote:

First of all, are you familiar with Key-Value Validation?
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/KeyValueCoding/Concepts/Validation.html


So I'm looking for a way to safely and reliably determine whether a property will accept an object of a given type. I wrote this category on NSObject as a simple wrapper on the runtime to attempt this, but it doesn't work quite how I expected - any property that returns an object simply returns '@' as its return type - I can't tell what class it is. Can anyone point me in the right direction?

I don't believe this is possible. The "@" you're getting is the type encoding for an object pointer.

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/chapter_7_section_1.html

Note how it says "whether statically typed or typed id".

Basically, in Objective-C static typing is purely a compile-time thing. The compiler can use it to check the semantics of your program. However, it's "lost" after that. It's not baked into the resulting executable. Therefore, the Objective-C runtime can't access it. It just isn't there.


I think your only avenue is to implement explicit KVV methods for your properties which have the necessary knowledge to test their type. I suppose you could file a feature request to extend declared properties so they can synthesize a type-checking validation method. Or request a compile-time directive to determine the static type of a method return or a property. (Hmm. gcc has a "typeof" extension. I wonder if it works in Objective-C.)


Ken, I was only vaguely aware of what KVV was for - it looks like the right way to handle this. So I'll investigate that approach - now I come to read the docs in detail, it definitely sounds like what I need. It will certainly do the trick to prevent a bad property type being set, though that's a little bit after the fact. It's not quite so clear how I could validate a type ahead of time so that I could filter out inappropriate ones - but I'll give it some more thought.

On the second point I think you're entirely right - I sort of suspected that might be the case as soon as I saw the results I was getting.

thanks, Graham



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to