gpt4 book ai didi

ios - 使用特定键对NSArray进行排序

转载 作者:行者123 更新时间:2023-12-01 19:55:00 25 4
gpt4 key购买 nike

我已从服务器请求一个数组,现在我想过滤所有在新数组中具有"status_read": 1的数据。我该怎么做?这是我的数据响应:

{
"status": "success",
"message": "success",
"data": [
{
"logo": "http://**.com/timepicker/images/default_o.jpg",
"title": "Title 1",
"icon": "https://**.com/admin/dashboard/dist/img/a.png",
"type": 1,
"link_detail": "",
"id_call": 12,
"status_read": 1
},
{
"logo": "http://**.com/timepicker/images/default_go.jpg",
"title": "Title 2",
"icon": "https://**.com/admin/dashboard/dist/img/b.png",
"type": 0,
"link_detail": "http://google.com",
"id_call": 0,
"status_read": 0
}
]
}

这是我的过滤器数组代码:
NSString *dictionaryKey = @"status_read";
NSInteger predicateValue = 1;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %i", dictionaryKey, predicateValue];
NSArray *filteredArr = [readNotif filteredArrayUsingPredicate:predicate];
generalArray = filteredArr;
[self.notifTableView reloadData];

最佳答案

NSString *jsonStr = @"{\"status\":\"success\",\"message\":\"success\",\"data\":[{\"logo\":\"http://**.com/timepicker/images/default_o.jpg\",\"title\":\"Title 1\",\"icon\":\"https://**.com/admin/dashboard/dist/img/a.png\",\"type\":1,\"link_detail\":\"\",\"id_call\":12,\"status_read\":1},{\"logo\":\"http://**.com/timepicker/images/default_go.jpg\",\"title\":\"Title 2\",\"icon\":\"https://**.com/admin/dashboard/dist/img/b.png\",\"type\":0,\"link_detail\":\"http://google.com\",\"id_call\":0,\"status_read\":0}]}";

NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//orignal array
NSArray *array = jsonDict[@"data"];
//create predicate to filter the array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status_read == 1"];

NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered Array: %@",filteredArray);

关于ios - 使用特定键对NSArray进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43110289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com