在解开可选值 keyboardWillShow 时意外发现 nil [英] unexpectedly found nil while unwrapping an Optional value keyboardWillShow
问题描述
我在下面有这个代码,当调用 keyboardWillShowNotification 时运行:
func keyboardWillShow(_ notification: Notification) {//错误在下一行键盘 = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValueanimaton = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValueUIView.animate(withDuration: 0.4, animations: { () -> Void inself.scrollView.frame.size.height = self.scrollViewHeight - self.keyboard.height})}
我在第二行收到一条错误消息:unexpectedly found nil while unwrapped an Optional value
.基本上,每当我单击 textFields 之一时,都会调用此键盘通知,并且 keyboardWillShow
中的代码将运行.我知道我放了 if...let
语句,但我想知道为什么我的结果为零.
我不确定我是如何收到此错误或如何调试它的.是不是因为我是在模拟器上运行的?
这是打印notification.userInfo 给出的内容:
<块引用>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: {1160,}4AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 568}, {320, 253}}, AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25])
来自文档:
let UIKeyboardFrameEndUserInfoKey: String
<块引用>
说明
一个 NSValue 对象的键,它包含一个标识以屏幕坐标表示的键盘结束帧
你的第二把钥匙:
let UIKeyboardAnimationDurationUserInfoKey: String
<块引用>
描述包含双精度值的 NSNumber 对象的键以秒为单位标识动画的持续时间.
因此您需要将第一个转换为 NSValue,将第二个转换为 NSNumber:
func keyboardWillShow(_ notification: Notification) {打印(键盘将显示")守卫让 userInfo = notification.userInfo else { return }键盘 = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValueanimaton = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue//你的代码}
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
})
}
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?
Here is what printing the notification.userInfo gives:
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])
From the docs:
let UIKeyboardFrameEndUserInfoKey: String
Description
The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates
Your second key:
let UIKeyboardAnimationDurationUserInfoKey: String
Description The key for an NSNumber object containing a double that identifies the duration of the animation in seconds.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!