以下是在Swift 3.0中的作品:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if context == &MyContext1 {
.........
}
else if context == &MyContext2 {
.........
}
}
但是,由于我有很多条件,如果我使用这样的开关/案例:
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
答案 0 :(得分:3)
解包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
}
}