当我遇到一个表达式时,我正在浏览HomeKit目录:创建家庭、配对和控制附件以及设置触发器的旧代码,该表达式如下
.KeyPathExpressionType我不知道这是什么
.在……里面
.KeyPathExpressionType是指在
.当我搜索Google和堆栈overflow以获得"KeyPathExpressionType“时,我什么也找不到。这和
.ConstantValueExpressionType我什么都没找到。
每一种平等比较
comparison.leftExpression.expressionType == .KeyPathExpressionType和
comparison.rightExpression.expressionType == .ConstantValueExpressionType在下面的代码中,生成一条错误消息,内容如下:
二进制运算符'==‘不能应用于'NSExpression.ExpressionType’和'_‘类型的操作数
extension NSPredicate {
/**
Parses the predicate and attempts to generate a characteristic-value `HomeKitConditionType`.
- returns: An optional characteristic-value tuple.
*/
private func characteristic() -> HomeKitConditionType? {
guard let predicate = self as? NSCompoundPredicate else { return nil }
guard let subpredicates = predicate.subpredicates as? [NSPredicate] else { return nil }
guard subpredicates.count == 2 else { return nil }
var characteristicPredicate: NSComparisonPredicate? = nil
var valuePredicate: NSComparisonPredicate? = nil
for subpredicate in subpredicates {
if let comparison = subpredicate as? NSComparisonPredicate, comparison.leftExpression.expressionType == .KeyPathExpressionType && comparison.rightExpression.expressionType == .ConstantValueExpressionType {
switch comparison.leftExpression.keyPath {
case HMCharacteristicKeyPath:
characteristicPredicate = comparison
case HMCharacteristicValueKeyPath:
valuePredicate = comparison
default:
break
}
}
}
if let characteristic = characteristicPredicate?.rightExpression.constantValue as? HMCharacteristic,
characteristicValue = valuePredicate?.rightExpression.constantValue as? NSCopying {
return .Characteristic(characteristic, characteristicValue)
}
return nil
}当我替换
comparison.leftExpression.expressionType == .KeyPathExpressionType使用
comparison.leftExpression.expressionType.rawValue == NSExpression.ExpressionType.keyPath.rawValue和
comparison.rightExpression.expressionType == .ConstantValueExpressionType使用
comparison.rightExpression.expressionType.rawValue == NSExpression.ExpressionType.constantValue.rawValue这是正确的吗?有人能告诉我关于这个问题的启示吗?
发布于 2019-05-22 12:52:26
代码是过时的Swift 2代码。
将.KeyPathExpressionType替换为.keyPath,.ConstantValueExpressionType替换为.constantValue
类型为NSExpression.ExpressionType,请阅读文档
https://stackoverflow.com/questions/56257043
复制相似问题