gpt4 book ai didi

ios - 从 [NSString : AnyObject] as UIViewAnimationCurve? 获取对象

转载 作者:行者123 更新时间:2023-11-28 13:16:28 25 4
gpt4 key购买 nike

我有以下代码:

    let userInfo = notification.userInfo

let animationCurve = userInfo?[UIKeyboardAnimationCurveUserInfoKey] as UIViewAnimationCurve
let animationDuration = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as NSTimeInterval
let keyboardFrame = userInfo?[UIKeyboardFrameEndUserInfoKey] as CGRect

userInfo 是一个 [NSObject: AnyObject]? 字典,UIKeyboardAnimationCurveUserInfoKey 键都是 NSString! .现在我想从这本词典中获取一些元素。但似乎并非我要使用的所有项目都继承自 AnyObjectUIViewAnimationCurve 是一个 enumCGRect 是一个 struct

我正在从 Objective-C 翻译这段代码,其中使用了以下语法:

[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];

现在我想知道如何在不使用(不安全)指针的情况下从字典中获取这些元素。

最佳答案

UIKeyboardFrameEndUserInfoKey 的值是一个 NSValue 代表一个 CGRect:

if let tmp = userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardFrame = tmp.CGRectValue()
}

UIKeyboardAnimationCurveUserInfoKey 的值是一个NSNumber表示枚举的整数值:

if let tmp = userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
let animationCurve = UIViewAnimationCurve(rawValue: tmp.integerValue)!
}

这可以组合成单个表达式,使用 nil-coalescingoperator ?? 提供默认值:

let keyboardFrame = (userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() ?? CGRectZero
let animationCurve = UIViewAnimationCurve(rawValue:
(userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.integerValue ?? 0
) ?? .Linear

关于ios - 从 [NSString : AnyObject] as UIViewAnimationCurve? 获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com