Scripting: Dynamic properties possible (straightforwardly)?
Scripting: Dynamic properties possible (straightforwardly)?
- Subject: Scripting: Dynamic properties possible (straightforwardly)?
- From: Luke Evans <email@hidden>
- Date: Sat, 19 Jan 2008 15:48:29 -0800
I have an object that (amongst other things) can hold a set of
'variables'. These are simply name/value pairs, and the value is
polymorphic (in fact, they are NSMutableObjects with a 'value' field
that corresponds to the storage types in Core Data).
The behaviour of the 'variables' is supposed to be simple. Ergo, if
you ask for a variable by name that doesn't exist, you get nil, but
you can set a value to a name that doesn't exist, in which case a new
variable is added to the set, with the right value type (i.e the
construction of the variable is hidden/automatic).
In the basic code I have chosen (for convenience I thought) to
implement a form of dynamic key on the container object for the
variables, such that it responds to valueWithKey: and setValue:withKey
for any key path that is prefixed by "variable.". Whether or not this
is a great idea is another matter, but it allows the container object
to react directly to values, rather than introducing another level of
object the 'variable' itself, which is then indirected to get the
value properly (though this is exactly what happens in the model - the
dynamic key stuff is just a short-cut).
Now that I'm going through adding scriptability for this stuff, I'm
somewhat hoisted by my own petard. In order to mirror the convenience
of being able to get/set variables on the top level container object,
I would need to be able to describe dynamic keys in my sdef file, and
then (presumably) my existing KVC accessors would work, so long as the
key was allowed to be of the right form "variable.<varName>" - though
this might itself be a pipedream.
If I go more with the flow of regular scripting, then I suppose I
ought to expose the 'variables' layer, so you could get the variable
with a specific name, and then set its value. However, setting then
becomes awkward, because variables do not exist until they've had a
value set, and I'd rather not have the scripting interface require a
'make variable X' before you can obtain a variable and set its value.
So, (as well as general uncertainty about the best approach here), I'm
wondering if there is a way to describe to the scripting system (in
the sdef) that a class will handle keys dynamically? I don't see a
way at the high-level, though I'm sure you can intercept low-level
apple events and do it there (!).
Other thoughts that occur to me:
- I could probably just use a command on the container object to
handle this:
e.g.: tell container to assign 3 to variable "foo" - or something
- I could possibly have a NULL variable that is returned in the
scripting case, if the name key is not found in the set of real
variables. This would allow scripting to set the value on this
variable, and the set accessor for the new value could ensure that a
new variable (i.e. name) is added to the live set of variables when set.
-- Lwe
_______________________________________________
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