如何从NSMutableArray获取状态为完整的最后一个元素?

时间:2016-07-16 06:40:29

标签: ios objective-c nsarray

我有一系列以下格式的词典

[
{
 "status":"not completed",
 "rating":2 
},{
"status":"completed",
 "rating":2 
},{
"status":"not completed",
"rating":"<null>" 
},{
 "status":"completed",
 "rating":"<null>"},{
"status":"not completed",
"rating":"<null>" 
}
]

从此我希望响应作为状态完成且评级为空的最后一个对象

output
{
     "status":"completed",
     "rating":"<null>"
}

如何在没有任何大迭代的情况下轻松获得第二个元素。

在实际情况中,此数组可能包含150-300个元素。

  -(void)checkRecentUnratedOrder:(NSMutableArray *)order{

    NSLog(@"trip log - %@",[order valueForKey:@"status"]); // total 5 objects inside array in that 4 of them are completed

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == completed"];
    NSArray *arr = [order filteredArrayUsingPredicate:predicate];

    NSLog(@"trip arr%@",arr);




}

控制台 enter image description here

1 个答案:

答案 0 :(得分:1)

您需要predicate这样的数组

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed'"];
NSArray *arr = [order filteredArrayUsingPredicate:predicate];
// Now `arr` contains all the `dictionary` that `status` key contain `completed` value. 
相关问题