How do I fix error: Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

How do I fix error: Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

当我 运行 进入一个表示

的表达式时,我正在检查 HomeKit Catalog: Creating Homes, Pairing and Controlling Accessories, and Setting Up Triggers 的旧代码
.KeyPathExpressionType

我不知道

.

.KeyPathExpressionType

指的是左边的

.

当我搜索 Google 和堆栈 溢出 时,我什么也没找到 "KeyPathExpressionType"。与

相同
.ConstantValueExpressionType

我什么也没找到。

每个相等比较

comparison.leftExpression.expressionType == .KeyPathExpressionType

comparison.rightExpression.expressionType == .ConstantValueExpressionType

在下面的代码中,生成一条错误消息:

Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

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

这是正确的吗?任何人都可以告诉我这个问题吗?

代码已过时Swift 2 个代码。

.KeyPathExpressionType 替换为 .keyPath,将 .ConstantValueExpressionType 替换为 .constantValue

类型为NSExpression.ExpressionType,请阅读documentation