Construct a property name programmatically

All,
Just out of curiosity, is it possible to construct a property name programmatically and use it to access that property’s methods,values ?

For example if I have 5 images connected in the IB and they are declared as follows:


property img1 : missing value
property img2 : missing value
property img3 : missing value
property img4 : missing value
property img5 : missing value

if I want to to hide all the images, I currently do :

img1's setHidden_(1)
img2's setHidden_(1)
img3's setHidden_(1)
img4's setHidden_(1)

I would like to do the following :

repeat with i from 1 to 5
set imgProperty to "img" & i -- construct property name
imgProperty's setHidden_(1)

end repeat

The above code obviously doesn’t work. But is this achievable somehow?

I don’t think you can do that. You could put each of the properties into an array, and then loop through calling setHidden_(1) on each.

Ric

You could do it with KVO, like this:

repeat with i from 1 to 5
my valueForKey_("img" & i)'s setHidden_(true)
end repeat

But it strikes me as an awfully ugly thing to do.

Shane, any particular reason why it would not be advisable? The solution seems to work.

I suppose if you had 50 of them, it might be justified. But for 5? All you would be doing is making your code harder to read to save two lines of code.

Easiest, shortest and fastest way:

Define a (boolean) property isHidden (or whatever you prefer) in your script
and bind the hidden property of all image views to isHidden.
Then changing the value of isHidden changes the visible state of the images