gpt4 book ai didi

ios - Swift(测试版 3) "NSDictionary? does not conform to protocol ' Equatable'"

转载 作者:可可西里 更新时间:2023-10-31 23:55:27 26 4
gpt4 key购买 nike

自从更新到最新的 Xcode 6 DP3 后,我的 Swift 代码中出现了一些警告和错误。大多数已通过采用新更改的语法得到解决,但有一个错误看起来很奇怪。

下面的代码给出了错误 Type 'NSDictionary?'不符合协议(protocol)“Equatable”:

if (launchOptions != nil && launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {

有人有解决办法吗?我可能在这里忽略了一些简单的事情......!

谢谢

最佳答案

Beta 3 中存在回归导致 Optional<T>无法与 nil 相提并论如果T不是 EquatableComparable .

这是一个由删除 _Nil 引起的错误为其定义相等运算符的类型。 nil现在是文字。该错误已由 Chris Lattner 在 Apple Dev Forums 上确认

一些解决方法:

您仍然可以使用 .getLogicValue()

if launchOptions.getLogicValue() && ... {

或直接

if launchOptions && ... { //calls .getLogicValue()

或者您可以使用“Javascript 对象到 bool 值”解决方案

var bool = !!launchOptions

(第一个 ! 调用 getLogicValue 并取反,第二个 ! 再次取反)

或者,您可以自己定义那些相等运算符,直到他们修复它为止:

//this is just a handy struct that will accept a nil literal
struct FixNil : NilLiteralConvertible {
static func convertFromNilLiteral() -> FixNil {
return FixNil()
}
}

//define all equality combinations
func == <T>(lhs: Optional<T>, rhs: FixNil) -> Bool {
return !lhs.getLogicValue()
}

func != <T>(lhs: Optional<T>, rhs: FixNil) -> Bool {
return lhs.getLogicValue()
}

func == <T>(lhs: FixNil, rhs: Optional<T>) -> Bool {
return !rhs.getLogicValue()
}

func != <T>(lhs: FixNil, rhs: Optional<T>) -> Bool {
return rhs.getLogicValue()
}

例子:

class A {
}

var x: A? = nil

if x == nil {
println("It's nil!")
}
else {
println("It's not nil!")
}

但是,此解决方法可能会导致其他细微问题(它可能与 Beta 2 中的 _Nil 类型类似,后者已被删除,因为它会导致问题......)。

关于ios - Swift(测试版 3) "NSDictionary? does not conform to protocol ' Equatable'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24638964/

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