Key-Value Coding (KVC) and Key-Value Observing (KVO) are based on Objective-C properties, so for something as simple as a one-line property declaration:
|property Name: NSString;|
|private fName: NSString;public[ObjectiveCMessage("name")]method getName: NSString;partial;[ObjectiveCMessage("setName:")]method setName(value: NSString);partial;|
|method Customer.getName: NSString;beginresult:= fName;end; method Customer.setName(value: NSString);begin WillChangeValueForKey("name"); fName := value; DidChangeValueForKey("name");end;|
With the new support for Aspect Oriented Programming (AOP) code-named “Cirrus” that we’re adding to Delphi Prism (Carlo just blogged about that earlier today), i though, why not wrap all of this up in a nice little aspect.
So i grabbed the latest internal Prism build (carlo had just finished integrating “Cirrus” into our builds), and just a a few minutes later, the [ObjectiveCProperty] aspect was born. It works pretty similarly to the plain attributes that Monobjc provides (such as [ObjectiveCMethod] you saw above), and with it, the above properly now looks like this:
|[aspect:ObjectiveCProperty]property Name: NSString;|
The aspect will work on any property, regardless of whether it uses shortcut declaration, a local field or manually specified getter/setter code. Just slap the aspect on, and the property is exposed to the Objective-C runtime. Optionally, the aspect can also take a different name to expose the property as (by default, it camel-cases the name, so “Foo” becomes “foo/setFoo”), and a boolean parameter to disable KVO.
And, most importantly, all of this is implemented in user code: anyone could write an aspect like this, even though the aspect accomplishes tasks that previously would have required extensive changes to the compiler!
As Carlo mentioned, “Cirrus” is still in development, and will ship in a future update to Delphi Prism later this year. Don’t quote me on it, but i would expect it to be available in the May release.