当我遇到一个表达式

.KeyPathExpressionType

我不知道
.

在里面
.KeyPathExpressionType

指的是
.

当我在Google和stackoverflow中搜索“KeyPathExpressionType”时,什么也找不到。这和
.ConstantValueExpressionType

我什么也没找到。
每一个平等的比较
comparison.leftExpression.expressionType == .KeyPathExpressionType


comparison.rightExpression.expressionType == .ConstantValueExpressionType

在下面的代码中,生成一条错误消息,指出:
二进制运算符“=”不能应用于“NSExpression.ExpressionType”和“uu”类型的操作数
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

10-08 09:14