【发布时间】:2015-11-03 13:02:57
【问题描述】:
这条线let userInfo = notification.userInfo as! NSDictionary我收到警告:Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails
我尝试使用let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject> 替换let userInfo = notification.userInfo as! NSDictionary。但我收到一个错误:Expected '>' to complete generic argument list。如何修复警告。
Xcode 7.1 OS X Yosemite
这是我的代码:
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()
}
}
【问题讨论】:
-
只使用 Swift 原生字典
-
我尝试使用
let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject>,但它是错误的,我得到一个错误。 -
为什么要进行类型转换?
标签: swift nsdictionary