Josh O'Connor
Josh O'Connor

Reputation: 4962

"No subscript members" error (Migrating to Swift 3)

Im currently migrating my code to Swift 3, and am coming across an error that I cannot figure out. The observeValue(forKeyPath function has been updated in Swift 3, and my code no longer works b/c of this. The error states "Type '[NSKeyValueChangeKey: Any]?' has no subscript members", and its pointed to the subscripting of the constant "change". Some reason in Swift 3, it won't let me subscript the change parameter. How do I fix this error?

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    //Line below is generating error: Type '[NSKeyValueChangeKey: Any]?' has no subscript members
    let change = change["new"] as? Float
}

Upvotes: 1

Views: 2031

Answers (1)

dan
dan

Reputation: 9825

The change parameter is an optional, so you have to unwrap it before you can subscript it. You should also use the enumeration value as the subscript instead of a raw string:

let change = change?[.newKey] as? Float

Upvotes: 6

Related Questions