我有字典的數組在下面的格式如何從狀態爲完成的NSMutableArray獲取最後一個元素?
[
{
"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);
}
開始和向後遍歷。只要您找到「狀態」爲「已完成」的元素,請將其返回。 – smarx