1

Here is the object, and have following attribute:

NSString attri1;
NSString attri2;
NSString attri3;
NSString attri4;

If I want to list these attri, I can call

NSLog(aObj.attri1);

But can I make the 1 as a variable to call it from a loop? Is this possible to do so in objective-c?

for(int i = 0; i < [array count]; i++)
{
    NSLog(aObj.attri1); //is this possible to become one line, dynamic generated variable
}

Thank you. btw, What is this feature called? Thanks.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • possible duplicate of [Objective C Equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), http://stackoverflow.com/questions/6846728/, http://stackoverflow.com/questions/3164860/, http://stackoverflow.com/questions/2484778/, http://stackoverflow.com/questions/6049175/, http://stackoverflow.com/questions/6164134/ – jscs Nov 12 '11 at 01:37

2 Answers2

3

If you want to dynamically access a property of an object, that can be done with Key Value Coding.

If the class is KVC-compliant, as most NS classes are, you can use valueForKey: or valueForKeyPath: to access a property with a string:

for(int i = 0; i < [array count]; i++) {
    NSLog([[aObj valueForKey:[NSString stringWithFormat:@"attrib%d", i]]);
}
chown
  • 51,908
  • 16
  • 134
  • 170
  • 2
    You *could* do this, but it smells a lot like an architectural problem if you do. It is exceptionally rare to have classes that have an arbitrary set of ivars that are accessed through the generic accessors. It is both slow and fragile. Far better to have a more formal, declarative, architecture. Or, alternatively, just use an NSMutableDictionary, potentially encapsulating it in your custom class to provide a clear delineation between "set of goo" and "well defined stuff". – bbum Nov 11 '11 at 07:38
  • I'm not endorsing this method of coding by any means; my goal was simply to answer the question accurately :). I do agree though that this style of coding is not favorable. – chown Nov 11 '11 at 19:47
  • 1
    Yup; Totally. Point awarded. :) Just... well.. Don't Do That! – bbum Nov 11 '11 at 20:36
1

The feature you're looking for is generally called "variable variables." Objective-C does not have this feature. Actually, most languages don't.

The good news is that you don't actually need this feature. Four variables named the same thing with a number at the end is basically equivalent to an array, only with the structure being implicit rather than explicit. Just make attri an array and then you can ask it for a numbered item.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • All of the scripting language do support it though, in one form or another (Even Python!). – cwallenpoole Nov 11 '11 at 07:20
  • 1
    @cwallenpoole: The "in one form or another" is rather important here. It's *possible* in Python, but this is not a Pythonic thing to do. It's also not idiomatic in Ruby. And even if it were something that's commonly done in those languages, I hardly see how it's relevant that a handful of languages very unlike Objective-C support it. – Chuck Nov 11 '11 at 19:25