gpt4 book ai didi

ios - 如何修复错误 : Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

转载 作者:行者123 更新时间:2023-11-28 13:39:04 24 4
gpt4 key购买 nike

我正在查看来自 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

关于ios - 如何修复错误 : Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56257043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com