0

I keep getting an Cannot convert expression's type '[NSObject : AnyObject]?' to 'NSDictionary' error and I don't know what to do. I tried everything, looked everywhere. Can you please help? I am creating a custom keyboard in SWIFT and I am totally new at this so i could definitely use the help.

    // Called when `UIKeyboardWillShowNotification` is sent.

func keyboardWillShow(aNotification: NSNotification) {
    let info = aNotification.userInfo as NSDictionary **<<<<<<<<<ERROR HERE>>>>>>>**
    let sizeBegin = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size
    let sizeEnd = info.objectForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue().size

    let duration = info.objectForKey(UIKeyboardAnimationDurationUserInfoKey).doubleValue
    let curve = info.objectForKey(UIKeyboardAnimationCurveUserInfoKey).integerValue


    var animationCurve: UIViewAnimationCurve
    if let value = UIViewAnimationCurve.fromRaw(curve) {
        animationCurve = value
    } else {
        animationCurve = UIViewAnimationCurve.EaseInOut
    }

    let insets = UIEdgeInsets(top: 44, left: 0, bottom: sizeEnd.height, right: 0)

    UIView.animateWithDuration(duration, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        self.textView.contentInset = insets
        self.textView.scrollIndicatorInsets = insets
    }, completion: nil)
}

// Called when `UIKeyboardWillHideNotification` is sent.
func keyboardWillHide(aNotification: NSNotification) {
    let info = aNotification.userInfo as NSDictionary **<<<<<<<<<<ERROR HERE>>>>>>**
    let sizeBegin = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size
    let sizeEnd = info.objectForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue().size

    let duration = info.objectForKey(UIKeyboardAnimationDurationUserInfoKey).doubleValue
    let curve = info.objectForKey(UIKeyboardAnimationCurveUserInfoKey).integerValue


    var animationCurve: UIViewAnimationCurve
    if let value = UIViewAnimationCurve.fromRaw(curve) {
        animationCurve = value
    } else {
        animationCurve = UIViewAnimationCurve.EaseInOut
    }

    let insets = UIEdgeInsets(top: 44, left: 0, bottom: sizeEnd.height, right: 0)

    UIView.animateWithDuration(duration, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        self.textView.contentInset = insets
        self.textView.scrollIndicatorInsets = insets
    }, completion: nil)
}
jscs
  • 63,694
  • 13
  • 151
  • 195
  • Similar question here: http://stackoverflow.com/questions/25381338/nsobject-anyobject-does-not-have-a-member-named-subscript-error-in-xcode. – Martin R Jan 17 '15 at 22:19

1 Answers1

0

Your dictionary is of type [NSObject:AnyObject]? which is an Optional type that must be unwrapped to be used. Since it is an Optional, it could be nil. One safe way of dealing with this is to use the nil coalescing operator ?? to unwrap it:

let info = (aNotification.userInfo ?? [:]) as NSDictionary

That will either unwrap your dictionary if it is not nil or give you a new empty dictionary if it is nil.

vacawama
  • 150,663
  • 30
  • 266
  • 294