这行让userInfo = notification.userInfo为! NSDictionary我得到一个警告:来自'[NSObject:AnyObject]?’不相关的类型’NSDictionary’总是失败 我尝试使用let userInfo = notification.userInfo作为!字典 NSObj
我尝试使用let userInfo = notification.userInfo作为!字典< NSObject:AnyObject>将let userInfo = notification.userInfo替换为!的NSDictionary.但我收到一个错误:预计’>’完成通用参数列表.如何修复警告.
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() } }NSNotification的userInfo属性已定义为(n可选)字典.
所以你根本不需要施放它,只需打开它.
func keyboardWillShow(notification: NSNotification) { if let userInfo = notification.userInfo { ... } }
所有其余的代码应该按原样运行.