Swift: NSNumber is not a subtype of UIViewAnimationCurve

How do I get the following line to compile?

UIView.setAnimationCurve(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)

This currently gives a compilation error:

'NSNumber' is not a subtype of 'UIViewAnimationCurve'

Why does the compiler think it userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValueis NSNumberwhen it is integerValuedeclared as Int?

class NSNumber : NSValue {
    // ...
    var integerValue: Int { get }`?
    // ...
}

I had similar problems with other types of enumerations. What is the general solution?

+4
source share
4 answers
UIView.setAnimationCurve(UIViewAnimationCurve.fromRaw(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!)

Read more: Swift and EnumerationsfromRaw()

UPDATE

Based on this answer to “How to use the default iIA7 curve of iOS7”, I use the new block-based animation method + animateWithDuration:delay:options:animations:completion:and get UIViewAnimationOptionslike this:

let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16))
+13

:

let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).unsignedIntegerValue
let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
+2

:

  • Optional numbers can be represented as NSNumbers, and the result of any index operation must be optional. In this case, integerValue is not an integer, it is an optional integer (trailing '?')

  • Enumerations are not interchangeable with integers.

Try:

UIAnimationCurve(userInfo[UIAnimationCurveUserInfoKey]?)
0
source

My decision

UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]!.integerValue)!)
0
source

Source: https://habr.com/ru/post/1545567/


All Articles