NSKeyValueChangeKey对成员下标的模糊引用

时间:2016-11-04 12:49:48

标签: swift3 xcode8

当我从swift 2转换为swift 3时 我所面临的错误是"对成员下标的模糊引用"

以下是我的代码:

class KeyValueChange {

    fileprivate let change: [String : AnyObject]

    init(change: [String : AnyObject]) {
        self.change = change
    }

    var kind: NSKeyValueChange? {
        guard let value = change[NSKeyValueChangeKey.kindKey] as? NSNumber else {
            return nil
        }

        return NSKeyValueChange(rawValue: value.uintValue)
    }

    var indexSet: IndexSet? {
        return change[NSKeyValueChangeKey.indexesKey] as? IndexSet
    }

    var new: AnyObject? {
        return change[NSKeyValueChangeKey.newKey]
    }

}

请详细说明,因为在很多地方都会遇到同样的错误

提前致谢

1 个答案:

答案 0 :(得分:0)

在Swift3中,KVO方法observeValue(forKeyPath:of:change:context:)[NSKeyValueChangeKey : Any]?参数传递change。如果您想使用KeyValueChange课程,您可以更好地适应变化:

class KeyValueChange {

    private let change: [NSKeyValueChangeKey : Any]

    init(change: [NSKeyValueChangeKey : Any]) {
        self.change = change
    }

    var kind: NSKeyValueChange? {

        guard let value = change[NSKeyValueChangeKey.kindKey] as? NSNumber else {
            return nil
        }

        return NSKeyValueChange(rawValue: value.uintValue)
    }

    var indexSet: IndexSet? {
        return change[NSKeyValueChangeKey.indexesKey] as? IndexSet
    }

    var new: Any? {
        return change[NSKeyValueChangeKey.newKey]
    }

}