7

Here's what I've tried so far

func onKeyboardRaise(notification: NSNotification) {
    var notificationData = notification.userInfo
    var duration = notificationData[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
    var frame = notificationData[UIKeyboardFrameBeginUserInfoKey]! as NSValue
    var frameValue :UnsafePointer<(CGRect)> = nil;
    frame.getValue(frameValue)
}

But I always seem to crash at frame.getValue(frameValue).

It's a little bit confusing because the documentation for UIKeyboardFrameBeginUserInfoKey says it returns a CGRect object, but when I log frame in the console, it states something like NSRect {{x, y}, {w, h}}.

Viper
  • 1,408
  • 9
  • 13
Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
  • 1
    It is not an unsafe pointer so don't turn it into one! Just read the docs on NSValue: https://developer.apple.com/library/ios/documentation/uikit/reference/NSValue_UIKit_Additions/Reference/Reference.html This is no different from doing it in Objective-C. No need to make easy things hard for yourself. – matt Aug 02 '14 at 04:55

1 Answers1

10

getValue() must be called with a pointer to an (initialized) variable of the appropriate size:

var frameValue = CGRect(x: 0, y: 0, width: 0, height: 0)
frame.getValue(&frameValue)

But it is simpler to use the convenience method:

let frameValue = frame.CGRectValue() // Swift 1, 2
let frameValue = frame.cgRectValue() // Swift 3
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This is awesome, thank you sir! I had trouble in the beginning because xCode doesn't autocomplete if you start typing "CGRec.." so I thought the method didn't exist. Another bug in xCode beta! – Tamby Kojak Aug 02 '14 at 14:32