我有一个NSArray
(DataArray),其响应如下:
{
"id": "17",
"item_name": "Chivito",
"item_price": "88.00",
"item_status": "Y",
"is_admin": "0",
"item_quantity": "120",
"status": 0
},
{
"id": "14",
"item_name": "Doughnut",
"item_price": "50.00",
"item_status": "Y",
"is_admin": "0",
"item_quantity": "100",
"status": 1
},
{
"id": "37",
"item_name": "Drink test ",
"item_price": "80.00",
"item_status": "Y",
"is_admin": "0",
"item_quantity": "12",
"status": 1
},
我必须创建另一个仅包含状态值为“1”的对象的过滤数组 请帮我怎么做
答案 0 :(得分:1)
试试这个
array.filter{(object) -> Bool in
if let object = object as NSDictionary {
if let status = object["status"] as? Int {
return status == 1
}
}
return false
}
答案 1 :(得分:0)
参考@Hady ans,在Objective-C中你可以这样做。如果有任何格式/语法问题,请原谅。我离开了我的系统。
NSMutableArry *filteredArr = [NSMutableArray alloc]init];
for(i=0;i<dataArray.count;i++)
{
if([DataArray[i][@"status"] isEqualToNumber:[NSNumber numberWithInt: 1]]) //if(dataArray[i]["status"] == 1)
{
[filteredArr addObject:dataArray[i]];
}
}
希望它有所帮助..快乐编码!!
答案 2 :(得分:0)
尝试谓词简单且循环次少。
NSPredicate * sizePredicate = [NSPredicate predicateWithFormat:@“状态 ==%@“,[NSNumber numberWithBool:YES]];
NSMutableArray * filteredSizeArray = [self.arrayFilter filteredArrayUsingPredicate:sizePredicate] .copy;
答案 3 :(得分:-1)
尝试:
<强>目的-C:强>
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.status == %d",1];
NSArray *filtered = [DataArray filteredArrayUsingPredicate:predicate];
NSLog(@"RESULT: %@", filtered);
答案 4 :(得分:-1)
你可以试试这个:
NSPredicate *prediate = [NSPredicate predicateWithFormat:@"status == %d",1];
arrResult = [myArray filteredArrayUsingPredicate:prediate];