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 }`?
    // ...
}
其他枚举类型也有类似的问题。一般解决方案是什么?

您有两个问题:

  • 可选数字可以表示为NSNumbers,任何下标操作的结果都应该是可选的。在本例中,integerValue不是整数,而是可选整数(尾随的“?”)

  • 枚举不能与整数互换

  • 尝试:

    阅读更多:

    更新 基于,我使用新的基于块的动画方法,
    +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)!)