Is this possible to call variable dynamically in Objective C?

后端 未结 2 1895
半阙折子戏
半阙折子戏 2020-12-20 08:11

Here is the object, and have following attribute:

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

If I want to list the

2条回答
  •  情歌与酒
    2020-12-20 08:22

    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]]);
    }
    

提交回复
热议问题