user3452
user3452

Reputation: 359

Filtering the NS array and getting the element according to the issynced value

I'm working in objective c. Here i need the array element which contains IsSynced = 0 in my NSlog. I need to filter the resultArry.

{
        ActivityTime = "2018-07-24T05:48:25.017Z";
        EntityId = "B88476DA-3707-4B37-A1E0-142875CFAE6E";
        ErrorCode = "<null>";
        ErrorData = "<null>";
        ErrorMessage = "<null>";
        IsSynced = 0;
        Message = "Waybill 'QBZZ222220180724111804' created.";
        PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
        PropertyPIC = QBZZ2222;
        RecentActivityId = "79A6E4D7-2D60-4635-9A70-C4E55D5A1399";
        TableNames = NVD;
    },
        {
        ActivityTime = "2018-07-23T11:31:25.905Z";
        EntityId = "23AE3D5E-8423-428F-9678-7BB3A75EF320";
        ErrorCode = "<null>";
        ErrorData = "<null>";
        ErrorMessage = "<null>";
        IsSynced = 1;
        Message = "Waybill 'QBZZ222220180723170035' created.";
        PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
        PropertyPIC = QBZZ2222;
        RecentActivityId = "2C3C7047-F43C-4521-8B66-419AF31EC766";
        TableNames = NVD;
    },


        {
        ActivityTime = "2018-07-23T09:44:32.483Z";
        EntityId = "49914A90-8F05-4479-B2C8-49C34637E70B";
        ErrorCode = "<null>";
        ErrorData = "<null>";
        ErrorMessage = "<null>";
        IsSynced = 1;
        Message = "Waybill 'QBZZ222220180723151237' created.";
        PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
        PropertyPIC = QBZZ2222;
        RecentActivityId = "DCCF5B03-7AD3-404F-A235-006DF3A37F97";
        TableNames = NVD;
    },

This is my code

SArray *resultArry =  [[DBHelper getSharedInstance] getRecordsBySQL:propertyQuery];

    //NSString *recentactivitylog=[[NSString alloc] initWithData:resultArry encoding:NSUTF8StringEncoding];

    NSLog(@"This is the eNVDS in Recent Activity:%@" ,resultArry);
    NSPredicate *notsynced = [NSPredicate predicateWithFormat:
                              @"resultArry.IsSynced = 1"];
    NSArray *notsyncedenvds = [resultArry filteredArrayUsingPredicate:notsynced];
    NSLog(@"This is the eNVDS in Recent Activity which is not synced:%@" ,notsyncedenvds);

I just need the array element which has the value IsSynced as 0 in my NSlog as you can see this element below.

{
        ActivityTime = "2018-07-24T05:48:25.017Z";
        EntityId = "B88476DA-3707-4B37-A1E0-142875CFAE6E";
        ErrorCode = "<null>";
        ErrorData = "<null>";
        ErrorMessage = "<null>";
        IsSynced = 0;
        Message = "Waybill 'QBZZ222220180724111804' created.";
        PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
        PropertyPIC = QBZZ2222;
        RecentActivityId = "79A6E4D7-2D60-4635-9A70-C4E55D5A1399";
        TableNames = NVD;
    }

I don't know what is wrong with my code I'm not getting any element after filtering the predicate. Sorry I'm new to Objective-c.

Upvotes: 0

Views: 37

Answers (2)

vadian
vadian

Reputation: 285064

The predicate is wrong. You must not include the array itself

NSPredicate *notsynced = [NSPredicate predicateWithFormat: @"IsSynced = 1"];

But as you need the item(s) with IsSynced = 0 then you have to filter fo that:

NSPredicate *notsynced = [NSPredicate predicateWithFormat: @"IsSynced = 0"];

In case the value for key IsSynced is a string you have to add single quotes

NSPredicate *notsynced = [NSPredicate predicateWithFormat: @"IsSynced = '0'"];

Upvotes: 1

sumit GUPTA
sumit GUPTA

Reputation: 26

     NSPredicate *notsynced = [NSPredicate predicateWithFormat:
                          @"IsSynced == 0 || IsSynced == %@",@"0"];
     NSArray *notsyncedenvds = [resultArry filteredArrayUsingPredicate:notsynced];
     NSLog(@"This is the eNVDS in Recent Activity which is not synced:%@" ,notsyncedenvds);

or

for fast enumeration with large amount of data

    NSMutableArray *arr = [[NSMutableAraay alloc]init];
    for(NSDictionary *dict in resultArry){
        NSString *IsSynced = [NSStrinf StringWithFormat:@"%@",[dict objectforkey:@"IsSynced"]];
        if([IsSynced integerValue] = 0){
        [arr addObject:dict];
        }
     }
     NSLog(@"IsSynced zero value array %@",arr);

Upvotes: 1

Related Questions