Sent from '[NSObject: AnyObject]?' to an unrelated type, "NSDictionary" always fails

This line let userInfo = notification.userInfo as! NSDictionaryI get a warning:Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails

I am trying to use let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject>replace let userInfo = notification.userInfo as! NSDictionary. But I get the error message: Expected '>' to complete generic argument list. How to fix a warning.

Xcode 7.1 OS X Yosemite

This is my code:

func keyboardWillShow(notification: NSNotification) {

    let userInfo = notification.userInfo as! NSDictionary //warning

    let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)

    let keyboardInputViewFrame = self.finishView!.frame

    let deltaY = keyboardBoundsRect.size.height

    let animations: (()->Void) = {

        self.finishView?.transform = CGAffineTransformMakeTranslation(0, -deltaY)
    }

    if duration > 0 {



    } else {

        animations()
    }


}
+4
source share
2 answers

The NSNotification custom property is already defined as an (n optional) dictionary.

So you don’t have to drop it at all, just unzip it.

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        ...
    }
}

all other code will work as is.

+4
source

You are trying to force an option on an NSDictionary. Try:

let userInfo = notification.userInfo! as NSDictionary

.

+3

Source: https://habr.com/ru/post/1614218/


All Articles