2016-11-16 56 views
-3

我只想從部分數組中獲取我的數組。獲取數組的1個部分

這是我的結果...

(
    { 
     foreName = FORENAME1; 
     gender = female; 
     id = 15; 
     key = 12345678901; 
     longName = LASTNAME1; 
     name = FORELAST1; 
    }, 
    { 
     foreName = FORENAME2; 
     gender = male; 
     id = 52; 
     key = 12345678902; 
     longName = LASTNAME2; 
     name = FORELAST2; 
    }, 
    { 
     foreName = FORENAME3; 
     gender = male; 
     id = 77; 
     key = 12345678903; 
     longName = LASTNAME3; 
     name = FORELAST3; 
    } 
) 

我如何搜索ID 77,並獲得ID 77類似於下面的示例全項陣列?

{ 
    foreName = FORENAME3; 
    gender = male; 
    id = 77; 
    key = 12345678903; 
    longName = LASTNAME3; 
    name = FORELAST3; 
} 

回答

0

這個結果是一個數組,你要誰的id == 77元......

- (NSDictionary *)elementInArray:(NSArray *)array withId:(NSNumber *)anId { 
    // there are a lot prettier ways, but this will be the clearest 
    for (NSDictionary *d in array) { 
     if ([d[@"id"] isEqual:anId]) return d; 
    } 
    return nil; 
} 

// call it like this 
NSArray *array = // the array logged in the OP 
NSDictionary result = [self elementInArray:array withId:@77]; 
NSLog(@"%@", result);