Swift 3.0 大小写切换中的 UnsafeMutableRawPointer
Swift 3.0 UnsafeMutableRawPointer in Case Switch
以下作品在 Swift 3.0 中:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if context == &MyContext1 {
.........
}
else if context == &MyContext2 {
.........
}
}
但是,由于我有很多条件,如果我这样使用switch/case:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
switch context {
case &MyContext1 :
........
case &MyContext2 :
........
}
}
我收到有关 UnsafeMutableRawPointer 无法转换为整数的错误。
声明:
private var MyContext1 = 0
private var MyContext2 = 0
解包时 context
代码编译正常:
private var MyContext1 = 0
private var MyContext2 = 0
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch context! {
case &MyContext1 : break
//
case &MyContext2 : break
//
default: break
}
}
以下作品在 Swift 3.0 中:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if context == &MyContext1 {
.........
}
else if context == &MyContext2 {
.........
}
}
但是,由于我有很多条件,如果我这样使用switch/case:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
switch context {
case &MyContext1 :
........
case &MyContext2 :
........
}
}
我收到有关 UnsafeMutableRawPointer 无法转换为整数的错误。
声明:
private var MyContext1 = 0
private var MyContext2 = 0
解包时 context
代码编译正常:
private var MyContext1 = 0
private var MyContext2 = 0
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch context! {
case &MyContext1 : break
//
case &MyContext2 : break
//
default: break
}
}