unexpectedly found nil while unwrapping an Optional value keyboardWillShow

后端 未结 2 939
孤独总比滥情好
孤独总比滥情好 2020-12-20 01:47

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

func keyboardWillShow(_ notification: Notification) {
    //ERROR IN THE L         


        
2条回答
  •  庸人自扰
    2020-12-20 02:12

    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
    }
    

提交回复
热议问题