NSPredicate 不适用于 CLCircularRegions 的过滤数组

NSPredicate not working on filtering array of CLCircularRegions

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

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

但是谓词总是returns空数组。有人可以帮忙吗?

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

由于 "identifier" 是 属性,您需要将其作为密钥传递。

我用字符串常量替换了 store.storeID,例如:

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

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

Printing description of predicateForRegionId:
identifier == 469

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

Printing description of predicateForRegionId:
identifier == "469"

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

NSPredicate 总是给出 false。您应该将 storeID 转换为字符串:

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