案例切换中的Swift 3.0 UnsafeMutableRawPointer

时间:2016-12-01 18:07:10

标签: ios xcode swift3 key-value-observing unsafe-pointers

以下是在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

1 个答案:

答案 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
    }
}