基于具有变量的特定条件获取对象的属性

时间:2016-03-08 10:16:34

标签: objective-c

我有一些if else从对象获取某些属性:

对象

NSString *firstValue;
NSString *secondValue;
NSString *thirdValue;
NSString *fourthValue;

我希望根据某些if条件获取属性。例如我需要的东西:

int condition;
NSString *attribute;

if(condition == 1)
    attribute = @"firstValue";
else if(condition == 2)
    attribute = @"secondValue";
else if(condition == 3)
    attribute = @"thirdValue";
else if(condition == 4)
    attribute = @"fourthValue";

NSString *value = object.attribute;

我可以在Objective C中执行此操作吗?

1 个答案:

答案 0 :(得分:0)

switch语句可以解决这个问题:

int condition;
NSString *attr;

//...

switch (condition) {
    case 1:
        attr = @"firstValue";
        break;
    case 2:
        attr = @"secondValue";
        break;
    case 3:
        attr = @"thirdValue";
        break;
}