Casting a string as an instance variable.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Casting a string as an instance variable.

sergio_101

I would like to do something like:

MyObject updateWIth: aDictionary.

then, inside the object, treat the keys as instance variables, and the values as values.. and do something like:

(key1 asInstanceVariable(??)): value1.

this seems like it should be possible, but i am just not getting it.

any ideas?

thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Casting a string as an instance variable.

Marcus Denker-4

> On 09 Jan 2015, at 18:21, sergio_101 <[hidden email]> wrote:
>
>
> I would like to do something like:
>
> MyObject updateWIth: aDictionary.
>
> then, inside the object, treat the keys as instance variables, and the values as values.. and do something like:
>
> (key1 asInstanceVariable(??)): value1.
>
> this seems like it should be possible, but i am just not getting it.
>
> any ideas?
>

the magic is #instVarNamed:put:

self instVarNamed: key1 put: value1

(keep in mind that this is slower than direkt access of instance variables)

        Marcus







Reply | Threaded
Open this post in threaded view
|

Re: Casting a string as an instance variable.

sergio_101
perfect! speed is not a huge issue, in that this will just be a set of methods that build data one time (sort of like a rails migration) and it will not be run again..

thanks!

On Fri Jan 09 2015 at 4:29:04 PM Marcus Denker <[hidden email]> wrote:

> On 09 Jan 2015, at 18:21, sergio_101 <[hidden email]> wrote:
>
>
> I would like to do something like:
>
> MyObject updateWIth: aDictionary.
>
> then, inside the object, treat the keys as instance variables, and the values as values.. and do something like:
>
> (key1 asInstanceVariable(??)): value1.
>
> this seems like it should be possible, but i am just not getting it.
>
> any ideas?
>

the magic is #instVarNamed:put:

self instVarNamed: key1 put: value1

(keep in mind that this is slower than direkt access of instance variables)

        Marcus