这行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优胜美地

这是我的代码:

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 {
        ...
    }
}

您的所有其他代码应按原样工作。

关于swift - 从 '[NSObject : AnyObject]?'转换为不相关的类型 'NSDictionary'总是失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33491553/

10-12 15:31