inesperadamente encontrado nulo ao desembrulhar um valor opcional keyboardWillShow
Eu tenho este código abaixo que é executado quando o keyboardWillShowNotification é chamado:
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
})
}
Estou recebendo um erro na segunda linha dizendo:unexpectedly found nil while unwrapping an Optional value
. Basicamente, sempre que eu clicar em um dos campos de texto, essa notificação para o teclado será chamada e o código emkeyboardWillShow
correrá. Eu sei que eu coloqueiif...let
declarações, mas quero saber por que estou ficando nulo por isso.
Não tenho certeza de como estou recebendo esse erro ou como depurá-lo. É porque estou executando o simulador?
Aqui está o que a impressão do notification.userInfo fornece:
Opcional ([AnyHashable ("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 315}, {320, 253}}, AnyHashable ("UIKeyboardIsLocalUserInfoKey"): 1, AnyHashable ("UIKeyboardBoundsUserInfoKey"): NSRect: 0, , {320, 253}}, AnyHashable ("UIKeyboardAnimationCurveUserInfoKey"): 7, AnyHashable ("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {160, 694.5}, AnyHashable ("UIKeyboardCenterEndUserInfoKey"): 44Point: NS1: UIKeyboardFrameBeginUserInfoKey "): NSRect: {{0, 568}, {320, 253}}, AnyHashable (" UIKeyboardAnimationDurationUserInfoKey "): 0,25])