获取数组数组中String值的索引

时间:2013-05-24 10:43:22

标签: ios

我有一个获得元素索引值的独特场景,其结构是

Array - 在Array中 - 在Dictionary

(
    {
        STS = OPEN;
        "STS_ICON" = "LIGHT_GREY";
    },
    "Headerquarter Planning"
),
    (
        {
        STS = INPR;
        "STS_ICON" = "LIGHT_BLUE";
    },
    "In Process"
),
    (
            {
        STS = COMP;
        "STS_ICON" = "LIGHT_GREEN";
    },
    Released
),
    (
            {
        STS = CANC;
        "STS_ICON" = "LIGHT_RED";
    },
    "ON HOLD - Call Transfer Delay"
 )
)

这个案例让我说我想要索引 @“ON HOLD - 呼叫转移延迟”字符串。

我试着像这样..

NSUInteger index;
if([listOfStatus containsObject: list.statusType])
{
    index = [listOfStatus indexOfObject: list.statusType];
}

其中list.statusType为@“ON HOLD - 呼叫转移延迟”。但在这里,我得到了“索引”一些奇怪的值15744929。

1 个答案:

答案 0 :(得分:1)

尝试

- (NSInteger)findIndexOfStatus:(NSString *)status
{

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Status"
                                                        ofType:@"json"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];

    NSArray *listOfStatus = [NSJSONSerialization JSONObjectWithData:data
                                                         options:0
                                                           error:nil];


    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSArray *evaluatedObject, NSDictionary *bindings) {

        //NSDictionary *dict = evaluatedObject[0];
        //return [dict[@"STS"] isEqualToString:status];

        NSString *statusType = evaluatedObject[1];
        return [statusType isEqualToString:status];

    }];

    return [listOfStatus indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [predicate evaluateWithObject:obj];
    }];
}

您可以通过调用

找到索引
NSInteger index = [self findIndexOfStatus:@"ON HOLD - Call Transfer Delay"];

我还注释了一个选项,以确定您是否要使用状态代码。

Status.json文件