Types Swift:NSNumber不是UIViewAnimationCurve的子类型
我如何获得下面的行进行编译Types Swift:NSNumber不是UIViewAnimationCurve的子类型,types,casting,enums,swift,nsnumber,Types,Casting,Enums,Swift,Nsnumber,我如何获得下面的行进行编译 UIView.setAnimationCurve(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue) 现在,它给出了编译错误: 'NSNumber' is not a subtype of 'UIViewAnimationCurve' 当integerValue被声明为Int时,为什么编译器认为userInfo[uikeyboardanimationcurveUserInfo].integerVa
UIView.setAnimationCurve(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)
现在,它给出了编译错误:
'NSNumber' is not a subtype of 'UIViewAnimationCurve'
当integerValue
被声明为Int
时,为什么编译器认为userInfo[uikeyboardanimationcurveUserInfo].integerValue
是一个NSNumber
class NSNumber : NSValue {
// ...
var integerValue: Int { get }`?
// ...
}
其他枚举类型也有类似的问题。一般解决方案是什么?您有两个问题:
+animateWithDuration:delay:options:animations:completion:
,并获得uiviewmanimationoptions
,如下所示:
let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16))
let options=uiviewmanimationoptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey]作为NSNumber)。integerValue我使用以下代码从用户信息字典构造动画曲线值:
let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
我的解决办法是
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]!.integerValue)!)
UIView.setAnimationCurve(UIViewAnimationCurve.fromRaw((用户信息[UIKeyboardAnimationCurveUserInfo]作为NSNumber.integerValue)!)
,syntactucally是可以的,从逻辑上讲并非绝对可以。谢谢!这也可以在没有NSNumber
的情况下工作。此解决方案导致了一个致命错误:在使用XCode 6 beta 6为我打开可选值时意外发现为零。您能用工作解决方案更新您的答案吗?谢谢。使用基于块的解决方案动画方法更好。但这里不需要左移,只需使用unsignedLongValue
而不是我理解文档的方式使用integerValue()任何NSUInteger
值都应该起作用,因为这是uiviewmanimationoptions
值的原始类型。因此,我建议使用unsignedLongValue
,这将导致Swift中的UInt
类型值(这将转换为NSUInteger
,因此具有与uiviewmanimationoptions
相同的类型)。希望这能解释原因。既然您已经问过了,我实际上想知道为什么您的解决方案首先会起作用^^
let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]!.integerValue)!)