There is a trick Flash Actionscript developers can do to refer to instance properties at runtime. I was wondering if anything similar existed in Objective-C
In actionscript we can do:
var thisObject;
for (var i=0; i<10; i++) {
thisObject = this["myInstanceProperty"+i];
thisObject.doSomething();
}
I thought there would be a method similar to this in Objective-C, but I can’t find anything mentioned anywhere. I’m looking for something along the lines of:
for (int i=0; i<10; i++) {
NSString *buttonName = [NSString stringWithFormat:@"button_%i", i];
id *thisButton = [self instancePropertyWithStringName:buttonName];
thisButton.label = @"button %i";
}
Can you see what I’m getting at? I have a xib linking views to IBOutlets, and I’d like to refer to those IBOutlets from within a for loop, so I can add properties to them dynamically at runtime.
Any ideas?
You can use the following if the
selfobject conforms toNSKeyValueCoding— which it does by default for its instance variables and properties.