-1

this is my NSMutableArray format and this is working fine and am getting correct response also

[
    {
        "status":"not completed",
        "rating":2 
    },{
        "status":"completed",
        "rating":2 
    },{
        "status":"not completed",
        "rating":"<null>" 
    },{
        "status":"completed",
        "rating":"<null>"
    },{
        "status":"not completed",
        "rating":"<null>" 
    }
]

and code is

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed' and self.rating == NULL"];

NSArray *arr = [order filteredArrayUsingPredicate:predicate];

but in some case my NSMutableArray may change to this form

[
    {
        "status":not completed,
        "rating":2 
    },{
        "status":not completed,
        "rating":2 
    },{
        "status":not completed,
        "rating":4 
    }
]

now my code is crashing here

NSArray *arr = [order filteredArrayUsingPredicate:predicate];

i have attached console screen here

crash

THERE WAS A MISTAKE IN CODE LIKE before it was NSDictionary *jorder = response; to use predicate i converted this to NSMutableArray *jorder = response;

enter image description here

Crash saying

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x13f209b60'
*** First throw call stack:
(0x1841e6db0 0x18384bf80 0x1841edc4c 0x1841eabec 0x1840e8c5c 0x1001354ac 0x100135278 0x1000aaf4c 0x1001549cc 0x100e55a7c 0x100e55a3c 0x100e5b4e4 0x18419cd50 0x18419abb8 0x1840c4c50 0x1859ac088 0x1893ae088 0x100125798 0x183c628b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Bangalore
  • 1,572
  • 4
  • 20
  • 50

2 Answers2

0

plz use this code

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed' AND self.rating == %@",@"<null>"];

NSArray *arr = [order filteredArrayUsingPredicate:predicate];

Result

enter image description here

in my debug its show result enter image description here

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
  • @Bangalore there must be other reason of crash see my updated answer. – balkaran singh Jul 16 '16 at 09:34
  • you are not checking my question , dude i have only these [ { "status":"not completed", "rating":2 },{ "status":"not completed", "rating":2 },{ "status":"not completed", "rating":4 } ] i dont know how u r getting completed in this – Bangalore Jul 16 '16 at 09:36
  • @Bangalore i check from your both array wait i show other array response. – balkaran singh Jul 16 '16 at 09:37
  • @Bangalore Because balkaran added as string and you getting nil object in your response from api. – Nirav D Jul 16 '16 at 09:39
  • @Nirav its ur code which is crashing from previous question – Bangalore Jul 16 '16 at 09:41
  • if you remove `self.rating` condition from the predicate it will work properly check it once. – Nirav D Jul 16 '16 at 09:43
  • @Bangalore see i use your array and get 0 result without crash. – balkaran singh Jul 16 '16 at 09:49
  • i removed rating from my existing code then its not crashing at all – Bangalore Jul 16 '16 at 10:00
  • @Bangalore: just small change will work without crash NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed' and self.rating = NULL"]; NULL is C pointers (type void *). and == checking for actual object pointer address. – kaushal Jul 16 '16 at 13:02
  • *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x15087ad70' – Bangalore Jul 19 '16 at 04:38
  • that means your are filtereding NSCFDictionary it is not a array @Bangalore – balkaran singh Jul 19 '16 at 04:58
  • Good So you finally found the issue @Bangalore :) – balkaran singh Jul 19 '16 at 05:14
  • is there any way to use predicate on NSdictionary or Convert NSDictionary to NSMutableArray? – Bangalore Jul 19 '16 at 05:17
  • can you send me your id "response" value @Bangalore – balkaran singh Jul 19 '16 at 05:33
  • { "completed_order_image_link" = ""; "created_at" = "2016-07-15T12:24:04.386+05:30"; "customer_id" = 483; "customer_mobile" = 00000000; id = 4574; "o_fare" = { "waiting_charge" = 0; "waiting_time" = 60; "waypoint_charge" = 0; }; "previous_due" = 0; rating = ""; "receiver_mobile_number" = 00000000; "receiver_name" = Delta; status = completed; "tracking_url" = ""; "updated_at" = "2016-07-15T12:28:32.958+05:30"; } – Bangalore Jul 19 '16 at 06:19
  • @balkaransingh have you seen response (above) – Bangalore Jul 19 '16 at 06:22
  • @Bangalore yes i see plz wait – balkaran singh Jul 19 '16 at 06:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117660/discussion-between-balkaran-singh-and-bangalore). – balkaran singh Jul 19 '16 at 06:47
0

please check this code

NSString *response=@"[{\"status\":\"not completed\",\"rating\":2},{\"status\":\"not completed\",\"rating\":2},{\"status\":\"not completed\",\"rating\":2},{\"status\":\"not completed\",\"rating\":2},{\"status\":\"not completed\",\"rating\":2}]";
    NSData *data=[response dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed' AND self.rating == %@",@"<null>"];

    NSArray *arr = [array filteredArrayUsingPredicate:predicate];

and array is

<__NSCFArray 0x7f880a5eda70>(
{
    rating = 2;
    status = "not completed";
},
{
    rating = 2;
    status = "not completed";
},
{
    rating = 2;
    status = "not completed";
},
{
    rating = 2;
    status = "not completed";
},
{
    rating = 2;
    status = "not completed";
}
)

it will be work fine

Ajharudeen khan
  • 246
  • 2
  • 10