在展开可选值keyboardWillShow时意外地发现了nil

编程入门 行业动态 更新时间:2024-10-25 12:21:33
本文介绍了在展开可选值keyboardWillShow时意外地发现了nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的代码在调用keyboardWillShowNotification时运行:

I have this code below which runs when the keyboardWillShowNotification is called:

func keyboardWillShow(_ notification: Notification) { //ERROR IN THE LINE BELOW keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue animaton = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue UIView.animate(withDuration: 0.4, animations: { () -> Void in self.scrollView.frame.size.height = self.scrollViewHeight - self.keyboard.height }) }

我在第二行收到错误说:在展开可选值时意外发现nil 。基本上每当我点击其中一个textFields时,都会调用键盘的这个通知,并且 keyboardWillShow 中的代码将会运行。我知道我把 if ... let 语句,但我想知道为什么我这样做为nil。

I am getting an error on the second line saying: unexpectedly found nil while unwrapping an Optional value. Basically whenever I click on one of the textFields this notification for the keyboard will be called and the code in keyboardWillShow will run. I know I put if...let statements but I want to know why I am getting nil for this.

我不知道我是如何得到此错误或如何调试它。是因为我是从模拟器运行的吗?

I am not sure how I am getting this error or how to debug it either. Is it because I am running it from the simulator?

这是打印notification.userInfo给出的内容:

Here is what printing the notification.userInfo gives:

可选([AnyHashable(UIKeyboardFrameEndUserInfoKey):NSRect:{{0,315},{320,253}},AnyHashable(UIKeyboardIsLocalUserInfoKey):1,AnyHashable(UIKeyboardBoundsUserInfoKey): NSRect:{{0,0},{320,253}},AnyHashable(UIKeyboardAnimationCurveUserInfoKey):7,AnyHashable(UIKeyboardCenterBeginUserInfoKey):NSPoint:{160,694.5},AnyHashable(UIKeyboardCenterEndUserInfoKey):NSPoint:{ 160,441.5},AnyHashable(UIKeyboardFrameBeginUserInfoKey):NSRect:{{0,568},{320,253}},AnyHashable(UIKeyboardAnimationDurationUserInfoKey):0.25])

Optional([AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 315}, {320, 253}}, AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1, AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {320, 253}}, AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7, AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {160, 694.5}, AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {160, 441.5}, AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 568}, {320, 253}}, AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25])

推荐答案

来自文档:

let UIKeyboardFrameEndUserInfoKey: String

描述

Description

包含CGR的NSValue对象的键在屏幕坐标中标识键盘的结束帧

The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates

您的第二个键:

let UIKeyboardAnimationDurationUserInfoKey: String

描述包含的双精度的NSNumber对象的键,以秒为单位标识动画的持续时间。

Description The key for an NSNumber object containing a double that identifies the duration of the animation in seconds.

所以你需要将第一个转换为NSValue,将第二个转换为NSNumber:

So you need to cast the first one to NSValue and the second one to NSNumber:

func keyboardWillShow(_ notification: Notification) { print("keyboardWillShow") guard let userInfo = notification.userInfo else { return } keyboard = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue animaton = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue // your code }

更多推荐

在展开可选值keyboardWillShow时意外地发现了nil

本文发布于:2023-11-26 14:09:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   发现了   意外地   nil   keyboardWillShow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!