0

Here's my JSON data

[{
    "id": 1,
    "name":"Soup",
    "price":200,
    "isOK":1
},
{
    "id": 2,
    "name":"Salad",
    "price":100,
    "isOK":0
}]

I have created JSONModel as follows

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@property (assign, nonatomic) BOOL isOK;
@end

Next,

NSArray* modelsArray = [ProductModel arrayOfModelsFromDictionaries:objects];

Now what I want is

if(isOK)
{
   // then add the model into the modelsArray
} else {
   // then don't add the model into the modelsArray
}

Is it possible to write this logic somewhere within the model file, without iterating the modelsArray and then removing the objects one by one on the basis of isOK ?

Haider
  • 4,961
  • 2
  • 18
  • 25

4 Answers4

2
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['isOK'] CONTAINS '1'"];
NSArray* filted_Array_OK = [modelarray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filted_Array_OK);

NSArray* filted_Array_NOT_OK = [modelarray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF['isOK'] CONTAINS '0'"]];
NSLog(@"%@",filted_Array_NOT_OK);

Here modelArray contains your all values isOK = 0 and isOK = 1

with this code you got 2 different array that contains 2 type value that are isOK = 1 and isOK = 0 now you can use directly array when you want to use .

Output : filted_Array_OK

 (
        {
        id = 1;
        isOK = 1;
        name = Soup;
        price = 200;
    },
        {
        id = 3;
        isOK = 1;
        name = peace;
        price = 900;
    }
)

filted_Array_NOT_OK

(
        {
        id = 2;
        isOK = 0;
        name = cake;
        price = 200;
    },
        {
        id = 4;
        isOK = 0;
        name = bread;
        price = 800;
    }
)
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
  • Ok, I was wondering if there could be a function in the model file, which would return true or false and would decide whether the object will be added in the array or not. Anyways thanks for the detailed answer. – Haider Oct 24 '16 at 15:04
  • @Haider i just give you 2 array so when ever you want use it you can easily access it . – Himanshu Moradiya Oct 25 '16 at 04:06
2

Yes, you can use NSPredicate though;

   NSArray* modelsArray = [ProductModel arrayOfModelsFromDictionaries:objects];    
  [NSPredicate predicateWithFormat:@"SELF.isOK == %@ && SELF.isOK != nil", @YES];
   NSArray* filtedArray = [modelsArray filteredArrayUsingPredicate:predicate];
PrafulD
  • 548
  • 5
  • 13
1
NSArray* modelsArray = [ProductModel arrayOfModelsFromDictionaries:objects];    
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isOK == 1"];
NSArray* filtedArray = [modelsArray filteredArrayUsingPredicate:predicate];
Bill Xie
  • 105
  • 6
  • Ok, I was wondering if there could be a function in the model file, which would return true or false and would decide whether the object will be added in the array or not. Thanks for the answer – Haider Oct 24 '16 at 15:04
0

Using Fast enumeration you can do this .

for (ProductModel* productModel in objects) {
  if(productModel.isOK){
   [modelarray addOjbect:productModel];
  }
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84