NSPredicate没有处理过滤CLCircularRegions数组

时间:2018-03-14 03:09:39

标签: ios objective-c nspredicate core-location

我想看看我的地区是否已被监控。为此,我从monitoredRegions获取CLLocationManager,然后使用谓词过滤器,例如:

NSArray *regionsBeingMonitored = [_sharedLocationManager.monitoredRegions allObjects];
NSPredicate *predicateForRegionId = [NSPredicate predicateWithFormat:@"identifier == %@", store.storeID];

CLRegion *someRegion = [[regionsBeingMonitored filteredArrayUsingPredicate:predicateForRegionId] firstObject];

以下是控制台的输出:

Printing description of regionsBeingMonitored:
<__NSArrayI 0x600000280fa0>(
CLCircularRegion (identifier:'486', center:<+18.45381450,+73.79211000>, radius:100.00m),
CLCircularRegion (identifier:'466', center:<+35.54023330,-97.52949170>, radius:100.00m),
CLCircularRegion (identifier:'469', center:<+35.54023330,-97.52949170>, radius:100.00m),
CLCircularRegion (identifier:'485', center:<+18.56226900,+73.91274500>, radius:100.00m),
CLCircularRegion (identifier:'475', center:<+18.52043030,+73.85674370>, radius:100.00m),
CLCircularRegion (identifier:'468', center:<+0.00000000,+0.00000000>, radius:100.00m),
CLCircularRegion (identifier:'484', center:<+18.55905150,+73.91125600>, radius:100.00m),
CLCircularRegion (identifier:'467', center:<+35.54023330,-97.52949170>, radius:100.00m)
)
Printing description of predicateForRegionId:
identifier == 469

但是谓词总是返回空数组。任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

我已将store.storeID替换为字符串常量,如:

NSPredicate *predicateForRegionId = [NSPredicate predicateWithFormat:@"identifier == %@", @"486"];

一切正常。您的storeIDNSNumber。因此,你有

  

打印predicateForRegionId的说明:
  identifier == 469

作为输出而不是(注意双引号)

  

打印predicateForRegionId的说明:
  identifier ==“469”

如果您将数字与字符串进行比较,

NSPredicate始终为false。您应该将storeID转换为字符串:

NSPredicate *predicateForRegionId = [NSPredicate predicateWithFormat:@"identifier == %@",
    [store.storeID description]];

答案 1 :(得分:-1)

尝试NSPredicate *predicateForRegionId = [NSPredicate predicateWithFormat:@"%K == %@", @"identifier", store.storeID];

由于&#34;标识符&#34;是属性,您需要将其作为密钥传递。