cud*_*mer 2 nsdictionary ios swift
自从更新到最新的Xcode 6 DP3以来,我在Swift代码中遇到了一些警告和错误.大多数已通过采用新更改的语法解决,但有一个错误似乎很奇怪.
以下代码给出了错误Type 'NSDictionary?' does not conform to protocol 'Equatable':
if (launchOptions != nil && launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
Run Code Online (Sandbox Code Playgroud)
有没有人有办法解决吗?我可能会在这里忽略一些简单的东西..!
谢谢
有一个在Beta 3的回归导致该Optional<T>不能比拟的nil,如果T不是Equatable或Comparable.
这是由删除_Nil定义了相等运算符的类型引起的错误.nil现在是文字.该问题已由Chris Lattner在Apple Dev论坛上得到证实
一些解决方法:
你仍然可以使用 .getLogicValue()
if launchOptions.getLogicValue() && ... {
Run Code Online (Sandbox Code Playgroud)
或直接
if launchOptions && ... { //calls .getLogicValue()
Run Code Online (Sandbox Code Playgroud)
或者您可以使用"Javascript对象布尔"解决方案
var bool = !!launchOptions
Run Code Online (Sandbox Code Playgroud)
(先!称之为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()
}
Run Code Online (Sandbox Code Playgroud)
例:
class A {
}
var x: A? = nil
if x == nil {
println("It's nil!")
}
else {
println("It's not nil!")
}
Run Code Online (Sandbox Code Playgroud)
但是,这种解决方法可能会导致其他微妙的问题(它可能类似于_NilBeta 2中的类型,因为它导致了问题而被删除了...).
| 归档时间: |
|
| 查看次数: |
3826 次 |
| 最近记录: |