简体   繁体   English

如何在Swift中从NSValue获取值?

[英]How to get a value from NSValue in Swift?

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) . 但我似乎总是在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}} . 这有点令人困惑,因为UIKeyboardFrameBeginUserInfoKey的文档说它返回一个CGRect对象,但是当我在控制台中记录frame时,它表示类似NSRect {{x, y}, {w, h}}

getValue() must be called with a pointer to an (initialized) variable of the appropriate size: 必须使用指向适当大小的(初始化)变量的指针调用getValue()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM