iosswiftconstantsnsexpressionswift-keypath

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


I am going through old code from HomeKit Catalog: Creating Homes, Pairing and Controlling Accessories, and Setting Up Triggers when I ran into an expression that says

.KeyPathExpressionType

I can't tell what the

.

in

.KeyPathExpressionType

is referring to on the left side of the

.

I find nothing when I search Google and stackoverflow for "KeyPathExpressionType". It's the same with

.ConstantValueExpressionType

I find nothing.

Each of those equality comparisons

comparison.leftExpression.expressionType == .KeyPathExpressionType

and

comparison.rightExpression.expressionType == .ConstantValueExpressionType

in the code below, generate an error message that says:

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
    }

I get the error to go away when I replace

comparison.leftExpression.expressionType == .KeyPathExpressionType

with

comparison.leftExpression.expressionType.rawValue == NSExpression.ExpressionType.keyPath.rawValue

and

comparison.rightExpression.expressionType == .ConstantValueExpressionType

with

comparison.rightExpression.expressionType.rawValue == NSExpression.ExpressionType.constantValue.rawValue

Is this correct? Can anyone tell me enlighten me on this problem?


Solution

  • The code is outdated Swift 2 code.

    Replace .KeyPathExpressionType with .keyPath and .ConstantValueExpressionType with .constantValue

    The type is NSExpression.ExpressionType, please read the documentation