自iOS9起,identifier
的{{1}}属性已被折旧。有没有其他方法来比较两个不同的HMAccessory实例?
答案 0 :(得分:4)
在iOS 9中确定HMAccessory的新方法是使用
@available(iOS 9.0, *)
@NSCopying public var uniqueIdentifier: NSUUID { get }
答案 1 :(得分:0)
有几种方法可以做到这一点。正如@Maria所提到的,从iOS9开始,你有NSUUID属性'uniqueIdentifier':
@property(readonly, copy, nonatomic) NSUUID *uniqueIdentifier NS_AVAILABLE_IOS(9_0);
此外,您可以使用serialNumber characteristicType来比较HMAccessories,但请记住:您应该检查附件的“可达性”和“阻止”状态。如果配件无法访问或被阻止,则您可能无法读取序列号。
最后,我在Objective C中包含了一个实用程序代码片段(想要Swift?只是问一下,或者更好的是,将它用作练习;)):
/ ** @discussion:如果附件无法访问,则返回空字符串。 无法获取无法访问状态下的实际序列号。* * /
[请引用您的信息]
+ (NSString *)getSerialNumberFromHMAccessory:(HMAccessory *)accessory {
if (!accessory || !accessory.reachable || accessory.isBlocked) {
return @"";
}
for (HMService *service in accessory.services) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", @"characteristicType", HMCharacteristicTypeSerialNumber];
NSArray *result = [service.characteristics filteredArrayUsingPredicate:predicate];
if (result && [result count] > 0 && [result[0] isKindOfClass:[HMCharacteristic class]]) {
HMCharacteristic *serialNumChar = (HMCharacteristic *)result[0];
NSString *serialNum = [serialNumChar valueForKey:@"value"];
if (serialNum && [serialNum length] > 0) {
//DDLogInfo(@"Found serial number: %@ for accessory named \"%@\"", serialNum, accessory.name);
return serialNum;
}
}
}
return @"";
}
答案 2 :(得分:-1)
好的想出了一个解决方案。有一个HMCharaceteristic
HMCharaceteristicTypeSerialNumber
。对HMAccessory
进行了扩展:
extension HMAccessory {
var serialNumber: String? {
get {
for service in services {
for characteristic in service.characteristics {
if characteristic.characteristicType == HMCharacteristicTypeSerialNumber {
return characteristic.value as? String
}
}
}
return nil
}
}
}
现在可以比较:
accessory1.serialNumber == accessory2.serialNumber
还有其他解决方案吗?