-1

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

数组 - 数组内 - 字典内

(
    {
        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 - Call Transfer Delay”字符串的索引

我试过这样..

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

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

4

1 回答 1

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文件_

于 2013-05-24T12:01:10.427 回答