HomeKit崩溃的原因:' - [__ NSDate长度]:但是Bool发送了

时间:2017-02-07 22:59:10

标签: ios swift homekit

tl; dr - 问题中的代码是正确的。另一方崩溃的原因。

挑战是控制Elgato Eve Energy HomeKit启用的插座。 直到代码开始的地方,一切正常,所以选择了正确的房间,配件等。代码确实关闭了插座,但之后应用程序崩溃了。

guard let services = accessory?.services else {
    print("No Service")
    return
}
for service in services {
    if service.serviceType == HMServiceTypeOutlet {
        for characteristic in service.characteristics {
            print(characteristic.characteristicType)
            if characteristic.characteristicType == HMCharacteristicTypePowerState {
                print(characteristic.metadata!)
                dump(characteristic)
                // Turning off the outlet
                characteristic.writeValue(false, completionHandler: { (error: Error?) -> Void in
                    if error == nil {
                        print("Yep")
                    } else {
                        print("Nop")
                    }
                })
            }
        }
    }
}

日志

00000023-0000-1000-8000-0026BB765291
00000025-0000-1000-8000-0026BB765291
[%@ Format: bool, Manufacturer Description: Power State ]
- <HMCharacteristic: 0x1702c23e0> #0
  - super: NSObject
00000026-0000-1000-8000-0026BB765291
E863F10A-079E-48FF-8F27-9C2605A29F52
E863F126-079E-48FF-8F27-9C2605A29F52
E863F10D-079E-48FF-8F27-9C2605A29F52
E863F10C-079E-48FF-8F27-9C2605A29F52
2017-02-07 23:19:59.725340 POS[499:100178] -[__NSDate length]: unrecognized selector sent to instance 0x170007710
2017-02-07 23:19:59.725767 POS[499:100178] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x170007710'
*** First throw call stack:
(0x188a291b8 0x18746055c 0x188a30268 0x188a2d270 0x18892680c 0x1895be458 0x1895be350 0x1895be698 0x1006328f8 0x1a249ac8c 0x199674f34 0x19966c588 0x1a249d194 0x101951258 0x101951218 0x10195eaec 0x101954ce0 0x10195f088 0x101960e2c 0x101960b78 0x187abb2a0 0x187abad8c)
libc++abi.dylib: terminating with uncaught exception of type NSException

符号

2017-02-08 10:17:47.804825 POS[602:171994] -[__NSDate length]: unrecognized selector sent to instance 0x1740169c0

iZettlePayments`-[NSDictionary(Fractionized) dateForKey:]:
0x100973d88 <+0>: stp x22, x21, [sp, #-48]!
0x100973d8c <+4>: stp x20, x19, [sp, #16]
0x100973d90 <+8>: stp x29, x30, [sp, #32]
0x100973d94 <+12>: add x29, sp, #32 ; =32
0x100973d98 <+16>: mov x19, x0
0x100973d9c <+20>: nop
0x100973da0 <+24>: ldr x20, #861056 ; (void *)0x00000001ad8966b8: NSDateFormatter
0x100973da4 <+28>: nop
0x100973da8 <+32>: ldr x21, #851248 ; "izDateFormatterUsingISO8601"
0x100973dac <+36>: mov x0, x2
0x100973db0 <+40>: bl 0x1009e9b54 ; symbol stub for: objc_retain
0x100973db4 <+44>: mov x22, x0
0x100973db8 <+48>: mov x0, x20
0x100973dbc <+52>: mov x1, x21
0x100973dc0 <+56>: bl 0x1009e9b30 ; symbol stub for: objc_msgSend
0x100973dc4 <+60>: mov x29, x29
0x100973dc8 <+64>: bl 0x1009e9b78 ; symbol stub for: objc_retainAutoreleasedReturnValue
0x100973dcc <+68>: mov x20, x0
0x100973dd0 <+72>: nop
0x100973dd4 <+76>: ldr x1, #849044 ; "valueForKey:"
0x100973dd8 <+80>: mov x0, x19
0x100973ddc <+84>: mov x2, x22
0x100973de0 <+88>: bl 0x1009e9b30 ; symbol stub for: objc_msgSend
0x100973de4 <+92>: mov x19, x0
0x100973de8 <+96>: mov x0, x22
0x100973dec <+100>: bl 0x1009e9b48 ; symbol stub for: objc_release
0x100973df0 <+104>: mov x0, x19
0x100973df4 <+108>: bl 0x1009e9b78 ; symbol stub for: objc_retainAutoreleasedReturnValue
0x100973df8 <+112>: mov x19, x0
0x100973dfc <+116>: nop
0x100973e00 <+120>: ldr x1, #851168 ; "dateFromString:"
0x100973e04 <+124>: mov x0, x20
0x100973e08 <+128>: mov x2, x19
0x100973e0c <+132>: bl 0x1009e9b30 ; symbol stub for: objc_msgSend
0x100973e10 <+136>: mov x29, x29
0x100973e14 <+140>: bl 0x1009e9b78 ; symbol stub for: objc_retainAutoreleasedReturnValue
0x100973e18 <+144>: mov x21, x0
0x100973e1c <+148>: mov x0, x19
0x100973e20 <+152>: bl 0x1009e9b48 ; symbol stub for: objc_release
0x100973e24 <+156>: mov x0, x20
0x100973e28 <+160>: bl 0x1009e9b48 ; symbol stub for: objc_release
0x100973e2c <+164>: mov x0, x21
0x100973e30 <+168>: ldp x29, x30, [sp, #32]
0x100973e34 <+172>: ldp x20, x19, [sp, #16]
0x100973e38 <+176>: ldp x22, x21, [sp], #48
0x100973e3c <+180>: b 0x1009e9ac4 ; symbol stub for: objc_autoreleaseReturnValue

浏览Elgato Eve services and characteristics,显示数组中的第一个特征是持有名称的特征,第二个特征是我需要操作的特征。打印的元数据显示其bool,并确认其Power State,并且其可读和可写,如之前链接的表中所述。 由于我写了一个错误的正确特征,插座关闭,但几秒钟后,应用程序崩溃。我删除了任何观察者并委托可能会干扰的方法。这真让我感到困惑。 此外,代码不会在iOS 9上崩溃,只有iOS 10.2,以及10.3 beta

解决方案

对不起家伙们,经过大量的咒骂,脚趾受伤,头发少了很多,24小时后,我找到了罪魁祸首。我使用支付设备iZettle,一旦将库添加到项目中,它就崩溃了。 iZettle的那些人承认他们在NSDictionary上有一个扩展

@interface NSDictionary (Fractionized)
    - (NSDate *)dateForKey:(id)aKey;
@end

这一个与HomeKit中的扩展名冲突,这就是问题的原因。结果是他们必须在扩展中添加前缀并再次编译库。

1 个答案:

答案 0 :(得分:1)

虽然符号化堆栈跟踪会有所帮助,但是日志中有一个符号可能会提供一个有用的提示:某些东西试图询问NSDate的长度。根据我的经验,当图书馆希望某些内容成为NSString并且即将解析它或打印它或者你有什么时,就会发生这种情况。

也许您知道NSDates的使用位置。它还有助于了解崩溃是否在执行上述代码的同一线程上。鉴于您遇到了轻微的延迟,很难说它是否与该代码有关!您的应用程序中的其他位置,您正在响应设备本身,为您提供状态更新,并且可能打印您认为是字符串但实际上是日期的值?