Variable Variables

Basically I need a variable with a number at the end of it, which allows me to not write code for each variable.

Example:


on buttonClicked:sender

  if sender's title() as integer = 1 then
    set myVariable1 to something
  else if sender's title() as integer = 2 then
    set myVariable2 to something
  else if sender's title() as integer = .....
  end if

end buttonClicked:

and this continues for each variable.

All I need is like a tag to the variable, so that I don’t have to write the code all the times.

I have already tried with an NSMutableArray:


set array to current application's NSMutableArray's alloc()'s init()
set variables to {myVariable1, myVariable2, myVariable3, myVariable4, myVariable5, myVariable6}
array's addObjectsFromArray:variables


set key to (sender's title() as integer)
set (array's objectAtIndex:key) to "some text"

but I get this error: *** -[MyClass mySelector:]: Couldn't set «class ocid» id «data optr000000001092D47CFF7F0000» to "some text". (error -10006)

as you have an index value I recommend to use an (Applescript) array rather than a couple of variables
For example if you have 4 elements


property myArray : missing value

on applicationWillFinishLaunching:aNotification
	set myArray to {missing value, missing value, missing value, missing value}
end applicationWillFinishLaunching:

and then

on buttonClicked:sender

if sender's title() as integer = 1 then
	set item 1 of myArray to "something"
else if sender's title() as integer = 2 then
	set item 2 of myArray to "something else"
else if sender's title() as integer = 3 then
	set item 3 of myArray to "still something else"
else if sender's title() as integer = 4 then
	set item 4 of myArray to "still something else yet"
end if

end buttonClicked:



or, if the same text is supposed to be written at a different index


on buttonClicked:sender
set theIndex to sender’s title() as integer
set item theIndex of myArray to “something”
end buttonClicked:

I would suggest to use an NSMutableDictionary instead of an NSMutableArray. The “key” for arrays has to be an integer, when using dictionary the key can be equivalent to the title of the button.

set variables to {myVariable1, myVariable2, myVariable3, myVariable4, myVariable5, myVariable6}
set titles to {"title 1", "title 2", "title 3", "title 4", "title 5", "title 6"}
set dict to current application's NSMutableDictionary's dictionaryWithObjects:variables forKeys:titles

--get variable
set myVar to dict's objectForKey:sender's title()
--set variable
dict's setObject:"some text" ForKey:sender's title()

p.s. I’m not behind a mac right now so forgive any typo’s I have made

Thanks to both!
I used DJ Bazzie Wazzie suggestion because it’s closer to what I needed.
Actually what would have been perfect was to use those variable normally, without calling every time “dict’s objectForKey:”. But never mind, it’s good even so.

Anyway, if the variables are outlets connected to something in IB, how can I create an Outlet Collection, like in Objective-C?

Ok I resolved.

Let’s say we have a function like this:


on setImages:sender  
  
   set key to sender's tag()  
   if  key = 1 then  
      imageView1's setImage:myImage  
   else if key = 2 then  
      imageView2's setImage:myImage  
   else if key = 3 then  
      imageView3's setImage:myImage  
   end if  
  
end setImages:  

The short version is this:


on setImages:sender   
      
    set key to sender's tag as integer  
    set keyPath to (current application's NSString's stringWithFormat:("imageView" & key & ".image"))  
    my setValue:myImage forKeyPath:keyPath    
  
end setImages: