-2

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

Abhi
  • 243
  • 4
  • 19
  • Does this help: https://stackoverflow.com/questions/25955162/how-to-filter-nsarray-in-swift ? – thwiegan Jun 26 '17 at 09:41
  • Read the docs... Read the other SO questions... https://stackoverflow.com/questions/110332/filtering-nsarray-into-a-new-nsarray-in-objective-c – Fogmeister Jun 26 '17 at 09:56

5 Answers5

1

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
}
Hady Nourallah
  • 442
  • 4
  • 12
0

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!!

Renascent
  • 184
  • 1
  • 3
  • 17
luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
  • Just because that is how it is done in Swift does not mean that it is done that way in Objective-C. – Fogmeister Jun 26 '17 at 10:17
  • Code above will give required results. I didnt just translated it. I really dont think it deserves a downvote!!! – luckyShubhra Jun 26 '17 at 10:41
  • thanks it works i replace if(dataArray[i]["status"] == 1) with if([DataArray[i][@"status"] isEqualToNumber:[NSNumber numberWithInt: 1]]) – Abhi Jun 26 '17 at 11:19
  • You are welcome. You can accept my ans if it helped you. Actually I were unaware of data format. Thanks for edit. – luckyShubhra Jun 26 '17 at 11:30
  • @luckyShubhra it might work. Just like the other 2 answers work. Except this answer has 8 lines of code as opposed to 2. But other than that. There are hundreds of other SO questions asking the exact same thing. So this question shouldn't exist at all. – Fogmeister Jun 26 '17 at 11:35
0

Try predicate its easy and less looping .

NSPredicate *sizePredicate = [NSPredicate predicateWithFormat:@"status == %@",[NSNumber numberWithBool: YES]];

NSMutableArray * filteredSizeArray = [self.arrayFilter filteredArrayUsingPredicate:sizePredicate].copy;

Community
  • 1
  • 1
Vaibhav Gaikwad
  • 396
  • 5
  • 13
-1

Try:

OBJECTIVE-C:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.status == %d",1];
    NSArray *filtered = [DataArray filteredArrayUsingPredicate:predicate];
    NSLog(@"RESULT: %@", filtered);
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
-1

You can try this:

NSPredicate *prediate = [NSPredicate predicateWithFormat:@"status == %d",1];
arrResult = [myArray filteredArrayUsingPredicate:prediate];
Son Pham
  • 1,516
  • 1
  • 14
  • 22