ios - 如何在 Swift 中从 NSValue 获取值?

标签 ios 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 对象,但是当我在控制台中记录 frame 时,它声明类似 NSRect {{x, y}, {w, h}}

最佳答案

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

关于ios - 如何在 Swift 中从 NSValue 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091765/

相关文章:

ios - NSBundle.mainBundle().pathForResource 返回 nil

ios - UIToolBar 背景透明

ios scrollview - subview 垂直越界

iphone - 延迟 UITableView 加载

iphone - NSValue 到 NSNumber 转换崩溃

ios - 如何在 UIScrollView 上设置 zoomScale 动画

swift - 如何使用 swift 为不透明度设置动画?

iOS 获取基于区域设置的号码描述

快速组合接收器接收值内存泄漏

ios - Swift - UIView 委托(delegate)协议(protocol)不返回值