I have an NSArray
(DataArray) whose response is as follows:
{
"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
},
I have to create another filtered array of only those object whose status value is "1" Please help me how to do it
try this out
array.filter{(object) -> Bool in
if let object = object as NSDictionary {
if let status = object["status"] as? Int {
return status == 1
}
}
return false
}
Referring to @Hady ans, In Objective-C you can do it as. Forgive if there are any formatting/syntax issues. I am away from my system.
NSMutableArry *filteredArray = [NSMutableArray alloc]init];
for (i=0;i<dataArray.count;i++)
{
if([dataArray[i][@"status"] isEqualToNumber:[NSNumber numberWithInt: 1]]) //if(dataArray[i]["status"] == 1)
{
[filteredArray addObject:dataArray[i]];
}
}
Hope it helps.. Happy Coding!!
Try predicate its easy and less looping .
NSPredicate *sizePredicate = [NSPredicate predicateWithFormat:@"status == %@",[NSNumber numberWithBool: YES]];
NSMutableArray * filteredSizeArray = [self.arrayFilter filteredArrayUsingPredicate:sizePredicate].copy;
Try:
OBJECTIVE-C:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.status == %d",1];
NSArray *filtered = [DataArray filteredArrayUsingPredicate:predicate];
NSLog(@"RESULT: %@", filtered);
You can try this:
NSPredicate *prediate = [NSPredicate predicateWithFormat:@"status == %d",1];
arrResult = [myArray filteredArrayUsingPredicate:prediate];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.