Objective C needs @selector() style operator for property strings

Originator:kusterer
Number:rdar://7289608 Date Originated:9-Oct-2009 09:54 AM
Status:Open Resolved:
Product:Developer Tools Product Version:n/a
Classification:Enhancement Reproducible:Not Applicable
 
When doing KVO/KVC, one often has to specify strings that are actually selector names, or key paths. Since they actually consist solely of known identifiers in a known format, it would be great if Objective C introduced a syntax construct like @selector() that allowed specifying a string like this in a way that the compiler knows it's supposed to be a key-path, and can perform some basic validation to catch typos.

This would only change the language, as the final result of this would still be an NSString constant, so compiled code would stay fully backwards compatible and deployable on other platforms, and there would be no performance hit at runtime. Suggested syntax:

[myTextView addObserver: self forKeyPath: @property(stringValue) options: 0 context: nil];

if( [keyPath isEqualToString: @property(selection.subView.stringValue)] )
    ...

It is possible to implement part of this using a macro:

#define PROPERTY(propName)    NSStringFromSelector(@selector(propName))

But this only works with a single property (and only if its getter is identical to the property name), and adds an unnecessary function call to each string specified this way.

Please give us a construct like this and help us write less buggy code. Thank you.

Comments

I couldn't get -Wundeclared-selector to work in a program that also had a few .c files.

I filed a bug about it. Do you have any suggestions?

See also: http://openradar.appspot.com/7295341

By DavidPhillipOster at 2009-10-12 17:56:44.451854 (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!