Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails

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

I try to use let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject> replace let userInfo = notification.userInfo as! NSDictionary. But I get an error :Expected '>' to complete generic argument list. How to fix the 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()
    }


}
like image 905
rose Avatar asked Mar 14 '23 06:03

rose


1 Answers

NSNotification's userInfo property is already defined as a(n optional) dictionary.

So you don't need to cast it at all, just unwrap it.

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

all the rest of your code should work as is.

like image 179
Tristan Burnside Avatar answered May 14 '23 01:05

Tristan Burnside