Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Convert the expression's type 'CGRect' to type to 'NSCopying!' in swift

Tags:

swift

Cannot Convert the expression's type 'CGRect' to type to 'NSCopying!' in swift I am trying to implement Keyboard notification in swift file.

// Called when the UIKeyboardDidShowNotification is sent.

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

but not sure why am I getting this error?

like image 598
Jigs Sheth Avatar asked Jan 11 '23 11:01

Jigs Sheth


1 Answers

You can't downcast the value you're pulling out of the dictionary to CGRect. It's an NSValue object, so you can easily get the rect value out of it with CGRectValue(). This should get you what you want.

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

    if let rectValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue {
        let kbSize:CGSize = rectValue.CGRectValue().size
    }
}
like image 101
Mick MacCallum Avatar answered Jan 12 '23 23:01

Mick MacCallum