If you don't require the level of dynamism that involves inspecting the properties of your object at run time, then an NSArray
is the most straightforward and readable way; as far as efficiency I guess make a dynamic array of type void *
and iterate through that would be maybe faster, but more work for you at a questionable speed payoff considering typical sizes.
for (UISlider *slider in @[self.slider1, self.slider2, self.slider3]) {
[slider addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
}
If you do need to investigate your properties, then I'd approach it much as the answer on the other question suggested. My answer at first suggested parsing the property attribute string to account for custom named properties but apparently KVC takes care of that for you.
unsigned int numberOfProperties;
objc_property_t *properties = class_copyPropertyList([self class], &numberOfProperties);
for (int i = 0; i < numberOfProperties; i++) {
objc_property_t property = propertieses[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
id valueForProperty = [self valueForKey:propertyName];
if ([valueForProperty isKindOfClass:[UISlider class]]) {
[(UISlider *)valueForProperty addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
}
}
free(properties);