如何从Swift中获取NSValue的值?
这是我迄今为止所尝试的
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) }
但我似乎总是在frame.getValue(frameValue)
崩溃。
这有点令人困惑,因为UIKeyboardFrameBeginUserInfoKey
的文档说它返回一个CGRect
对象,但是当我在控制台中loggingframe
时,它表示类似于NSRect {{x, y}, {w, h}}
。
必须使用指向适当大小的(初始化的)variables的指针调用getValue()
:
var frameValue = CGRect(x: 0, y: 0, width: 0, height: 0) frame.getValue(&frameValue)
但使用便捷方法更简单:
let frameValue = frame.CGRectValue() // Swift 1, 2 let frameValue = frame.cgRectValue() // Swift 3