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()
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With