【问题标题】:Filtering the NS array and getting the element according to the issynced value过滤NS数组,根据issynced值获取元素
【发布时间】:2018-07-24 06:40:26
【问题描述】:

我在目标 c 工作。这里我需要在我的 NSlog 中包含 IsSynced = 0 的数组元素。我需要过滤 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;
    },

这是我的代码

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);

我只需要我的 NSlog 中 IsSynced 值为 0 的数组元素,如下所示。

{
        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;
    }

我不知道我的代码有什么问题,过滤谓词后我没有得到任何元素。抱歉,我是 Objective-c 的新手。

【问题讨论】:

  • 你的 NSLog 有正确的值 IsSynced = 0;那么有什么问题呢?
  • @Vinodh 我没有得到值为 IsSynced = 0 的数组元素;它只是将 NSArray *notsyncedenvds 的元素作为 o 元素提供,而不是它应该获得值为 IsSynced = 0 的 1 元素;从数组元素列表中,并在 Nslog 中显示
  • 我刚刚提到了第二个数组元素作为我应该在我的 NSlog 中获取的示例。请查看我的代码@Vinodh
  • 那么你应该用 IsSynced == 0 而不是 IsSynced == 1 格式更改谓词

标签: ios objective-c arrays nslog


【解决方案1】:
     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);

用于大量数据的快速枚举

    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);

【讨论】:

    【解决方案2】:

    谓词错误。不能包含数组本身

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

    但是由于您需要带有IsSynced = 0 的项目,因此您必须对其进行过滤:

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

    如果键 IsSynced 的值是字符串,则必须添加单引号

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多