Func observeValueForKeyPath has changed

With Xcode 7.0 beta 3 (I think it happened even before that version) the method func observeValueForKeyPath has changed:

Old and printed in book:

New as of Xcode 7.0 beta 3:

The change is concerning the dictionary change that is now of optional type .

I get the change, but I can’t adjust for it in my exercise code. Allowing it to autocomplete with the [String: AnyObject] line gives me an error that Cannot subscript a value of type ‘[String : AnyObject]?’ with an index of type ‘String’. I haven’t seen a subscript error anywhere, but I’m thinking this is a relatively simple fix. Anyone gotten past this?

[code] override func observeValueForKeyPath(keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer) {
if context != &KVOContext {
//If the context doesn’t match, this message must be intended for our superclass.
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
return
}

//ERROR HERE
var oldValue: AnyObject? = change[NSKeyValueChangeOldKey]
if oldValue is NSNull {
oldValue = nil
}

        let undo: NSUndoManager = undoManager!
        print("oldValue=\(oldValue)")
        undo.prepareWithInvocationTarget(object!).setValue(oldValue,
            forKeyPath: keyPath!)
}

[/code]

That sure is a confusing error message! The key is of type String, and you are using a String index. What the hey, heck, and holler?!

What the error message should say is, “Please unwrap the optional before trying to subscript it”. Essentially, you can never subscript an optional type; you have to use an if-let to unwrap the optional, then subscript the unwrapped variable, e.g.:

[code]var data: [String: AnyObject]? = [
“hello”: “world”
]

if let validData = data {
println(validData[“hello”])
}[/code]

[quote=“7stud7stud”]That sure is a confusing error message! The key is of type String, and you are using a String index. What the hey, heck, and holler?!

What the error message should say is, “Please unwrap the optional before trying to subscript it”. Essentially, you can never subscript an optional type; you have to use an if-let to unwrap the optional, then subscript the unwrapped variable, e.g.:

[code]var data: [String: AnyObject]? = [
“hello”: “world”
]

if let validData = data {
println(validData[“hello”])
}[/code][/quote]

Thank you!

    override func observeValueForKeyPath(keyPath: String?,
                                         ofObject object: AnyObject?,
                                         change: [String : AnyObject]?,
                                         context: UnsafeMutablePointer<Void>)
    {
            if context != &KVOContext {
                //If the context doesn't match, this message must be intended for our superclass.
                super.observeValueForKeyPath(keyPath,
                                             ofObject: object,
                                             change: change,
                                             context: context)
                return
            }

        if let oldValue = change?[NSKeyValueChangeOldKey] {
            let undo: NSUndoManager = undoManager!
            undo.prepareWithInvocationTarget(object!).setValue(oldValue, forKeyPath: keyPath!)
        }