Cannot convert expression type to 'CGRect' to type 'NSCopying!' in quick

Cannot convert expression type 'CGRect' to type 'NSCopying!' in quick I'm trying to embed a Keyboard notification in a swift file.

// Called when UIKeyboardDidShowNotification is dispatched.

  func keyboardWasShown(aNotification :NSNotification)
    {
        var info = aNotification.userInfo
        var kRect:CGRect = info[UIKeyboardFrameBeginUserInfoKey] as CGRect
        var kbSize:CGSize = kRect.size

but not sure why i get this error?

+4
source share
2 answers

You cannot omit the value that you pull from the dictionary in CGRect. This is an NSValue object, so you can easily get the rect value from it using CGRectValue (). This should do what you want.

func keyboardWasShown(aNotification: NSNotification) {
    let info = aNotification.userInfo

    if let rectValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue {
        let kbSize:CGSize = rectValue.CGRectValue().size
    }
}
+6
source

userInfo, if, nil

func keyboardWasShown(aNotification: NSNotification) {
    if let info = aNotification.userInfo {
        var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
        // do something with keyboardFrame

    }
}
+3

Source: https://habr.com/ru/post/1543710/


All Articles