我有這樣的代碼低於當keyboardWillShowNotification被稱爲運行:unexpectedly found nil while unwrapping an Optional value
:意外地發現零而展開的可選值keyboardWillShow
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
})
}
我在第二行說得到一個錯誤。基本上每當我點擊其中一個textField時,這個鍵盤通知就會被調用,並且keyboardWillShow
中的代碼將會運行。我知道我把if...let
陳述,但我想知道爲什麼我沒有這個。
我不知道我是如何得到這個錯誤或如何調試它。是因爲我是從模擬器運行它嗎?
這裏是印刷notification.userInfo給出:
可選([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])
登錄'notification.userInfo'不要使用
as AnyObject
鑄造。查看實際存在的密鑰。 – rmaddy噢,讓我試試。 – CapturedTree
我在問題中添加了什麼打印出來。它顯示'UIKeyboardFrameEndUserInfoKey'可用。 – CapturedTree