我如何在iOS中从javascript做同样的事情

时间:2014-11-12 12:43:50

标签: arrays json ios7

function findId(data, idToLookFor) {
    var categoryArray = data.category;
    for (var i = 0; i < categoryArray.length; i++) {
        if (categoryArray[i].id == idToLookFor) {
            return(categoryArray[i].product);
        }
    }
}

var item = findId(data, 1);

1 个答案:

答案 0 :(得分:0)

为什么不使用库,例如​​:http://www.jsonmodel.com/

无论如何,这里是ObjectiveC中代码的版本:

+(id)findId:(NSMutableArray *)data idToLookFor:(NSNumber *)idToLookFor
{
    NSMutableArray *categoryArray = [data valueForKey:@"category"];
    for (int i = 0; i < categoryArray.count; i++) {
        if ([[categoryArray[i] valueForKey:@"id"] isEqualToValue:idToLookFor]) {
            return [categoryArray[i] valueForKey:@"product"];
        }
    }
    return nil;
}