如何在Swift中从NSValue获取值?

tam*_*jak 6 ios nsvalue swift

这是我到目前为止所尝试的内容

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)
}
Run Code Online (Sandbox Code Playgroud)

但我似乎总是崩溃frame.getValue(frameValue).

它有点令人困惑,因为文档UIKeyboardFrameBeginUserInfoKey说它返回一个CGRect对象,但是当我登录frame控制台时,它表示类似的东西NSRect {{x, y}, {w, h}}.

Mar*_*n R 10

getValue() 必须使用指向适当大小的(初始化)变量的指针调用:

var frameValue = CGRect(x: 0, y: 0, width: 0, height: 0)
frame.getValue(&frameValue)
Run Code Online (Sandbox Code Playgroud)

但使用便捷方法更简单:

let frameValue = frame.CGRectValue() // Swift 1, 2
let frameValue = frame.cgRectValue() // Swift 3
Run Code Online (Sandbox Code Playgroud)