Ios 如何修复错误:二进制运算符'==';无法应用于类型为';NSExpression.ExpressionType';和'_';
当我遇到一个表示Ios 如何修复错误:二进制运算符'==';无法应用于类型为';NSExpression.ExpressionType';和'_';,ios,swift,constants,nsexpression,swift-keypath,Ios,Swift,Constants,Nsexpression,Swift Keypath,当我遇到一个表示 .KeyPathExpressionType 我说不出是什么原因 . 在 指的是位于 . 当我搜索Google并堆叠“KeyPathExpressionType”时,我什么也找不到。对我来说也是 .ConstantValueExpressionType 我什么也没找到 每一项都是平等比较 comparison.leftExpression.expressionType == .KeyPathExpressionType 及 在下面的代码中,生成一条错误消息: 二进制运
.KeyPathExpressionType
我说不出是什么原因
.
在
指的是位于
.
当我搜索Google并堆叠“KeyPathExpressionType”时,我什么也找不到。对我来说也是
.ConstantValueExpressionType
我什么也没找到
每一项都是平等比较
comparison.leftExpression.expressionType == .KeyPathExpressionType
及
在下面的代码中,生成一条错误消息:
二进制运算符“==”不能应用于“NSExpression.ExpressionType”和“\u1”类型的操作数
当我替换时,错误会消失
comparison.leftExpression.expressionType == .KeyPathExpressionType
与
及
与
这是正确的吗?有人能告诉我这个问题的答案吗?代码已经过时了Swift 2代码 将
.KeyPathExpressionType
替换为.keyPath
并将替换为.ConstantValueExpressionType
类型是NSExpression.ExpressionType
,请阅读不确定,但看起来像。KeyPathExpressionType
是的旧名称。keyPath
。你能试试比较一下吗?leftExpression.expressionType==.keyPath
?是的。这很有效。我实际上输入了“NSExpression.ExpressionType.keyPath”,我使用了“NSExpression.ExpressionType.keyPath”和“NSExpression.ExpressionType.constantValue”,它就工作了。谢谢。您可以省略NSExpression.ExpressionType
,编译器将推断类型。
comparison.rightExpression.expressionType == .ConstantValueExpressionType
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