如何从 NSMutableArray 中获取最后一个元素的状态为完成? [英] How to get last element from NSMutableArray in which status as complete?

查看:62
本文介绍了如何从 NSMutableArray 中获取最后一个元素的状态为完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的字典数组

<预><代码>[{"状态":"未完成",评分":2},{"状态":"已完成",评分":2},{"状态":"未完成",评级":"},{"状态":"已完成","评级":""},{"状态":"未完成",评级":"}]

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

输出{"状态":"已完成",评级":"}

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

在实际情况下,这个数组可能包含 150-300 个元素.

 -(void)checkRecentUnratedOrder:(NSMutableArray *)order{NSLog(@"行程日志 - %@",[order valueForKey:@"status"]);//数组内共有 5 个对象,其中 4 个已完成NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == completed"];NSArray *arr = [顺序过滤的ArrayUsingPredicate:谓词];NSLog(@"trip arr%@",arr);}

控制台

解决方案

你需要像这样predicate你的数组

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed'"];NSArray *arr = [顺序过滤的ArrayUsingPredicate:谓词];//现在`arr` 包含所有`status` 键包含`completed` 值的`dictionary`.

I have an array of dictionaries in following format

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

and from this i want response as last object where status is completed and rating is null

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

how do i get second element easily without any big iteration.

in real case this array might contains 150-300 elements.

  -(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);




}

console

解决方案

You need to predicate your array like this

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

这篇关于如何从 NSMutableArray 中获取最后一个元素的状态为完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆