下面的代码在调用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
发布评论