gpt4 book ai didi

swift - 从 '[NSObject : AnyObject]?' 转换为无关类型 'NSDictionary' 总是失败

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:47 26 4
gpt4 key购买 nike

这一行 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 属性已经被定义为一个(可选的)字典。

所以你根本不需要转换它,只需要打开它。

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

您的所有其余代码都应该按原样工作。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com