【问题标题】:NSPredicate to filter beacon according to UUID, major and minor raising exceptionNSPredicate 根据 UUID 过滤信标,主要和次要引发异常
【发布时间】:2019-01-16 22:13:27
【问题描述】:

我有一个 beacons 数组,其中包含 CLBeacon 的集合,我只想获取与 NSPredicate 中给定的 uuid、主要和次要匹配的信标。下面是对象 C 中的代码,由于谓词中的 UUID 而生成异常。如果我从查询中删除 uuidPredicate,代码可以正常工作。

 - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region{

        NSPredicate *uuidPredicate = [NSPredicate predicateWithFormat:@"uuid.UUIDString == [c] %@", @"03672ce6-9272-48ea-ba54-0bf679217980"];
       //NSPredicate *uuidPredicate = [NSPredicate predicateWithFormat:@"uuid == %@", @"03672ce6-9272-48ea-ba54-0bf679217980"];
        NSPredicate *majorPredicate = [NSPredicate predicateWithFormat:@"major = %ld", 1];
        NSPredicate *minorPredicate = [NSPredicate predicateWithFormat:@"minor = %ld", 3];

        NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[uuidPredicate, majorPredicate, minorPredicate]];

        NSArray *pointABeacon = [beacons filteredArrayUsingPredicate:compoundPredicate];   

    }

信标数组类似于

beacons (
    "CLBeacon (uuid:03672CE6-9272-48EA-BA54-0BF679217980, major:1, minor:1, proximity:1 +/- 0.07m, rssi:-61)",
    "CLBeacon (uuid:03672CE6-9272-48EA-BA54-0BF679217980, major:1, minor:2, proximity:1 +/- 0.07m, rssi:-62)",
    "CLBeacon (uuid:03672CE6-9272-48EA-BA54-0BF679217981, major:1, minor:3, proximity:2 +/- 1.64m, rssi:-53)"
)

例外是

*** 由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[ valueForUndefinedKey:]:这个类是 不符合键 uuid 的键值编码。'

如何过滤包含三个参数uuid、major和minor的数组?

【问题讨论】:

  • @"uuid.UUIDString == [c] => @ proximityUUID.UUIDString == [c]?只需执行CLBeacon *beacon = beacons.firstObject。并尝试写NSUUID *uuidTest = beacon.uuid XCode 对此有何评论? UUID 的属性名称是什么?

标签: nspredicate ibeacon nscompoundpredicate clbeacon


【解决方案1】:

错误很明显: CLBeacon 对象没有名为 uuid 的属性。

当您打印CLBeacon 对象时,您可能会看到“uuid”,但这不是属性的真实名称,它是proximityUUID

所以:

NSPredicate *uuidPredicate = [NSPredicate predicateWithFormat:@"uuid.UUIDString == [c] %@", @"03672ce6-9272-48ea-ba54-0bf679217980"];

应该是:

NSPredicate *uuidPredicate = [NSPredicate predicateWithFormat:@"proximityUUID.UUIDString == [c] %@", @"03672ce6-9272-48ea-ba54-0bf679217980"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多