Is a giant switch/case the right way to convert NSNumber to a generic type `T`?

:+1::pray::bowing_woman:t2: You solved my two problems:

  1. I only need to handle Numeric types, so <T: Numeric> is good enough
  2. The NSNumber comes from the Formatter of TextField, I got rid of the convert and just do cast directly, the as! T should be safe since T: Numeric?:
        @objc func doneAction(_ button: UIBarButtonItem) {
            field = (formatter.number(from: textField.text ?? "0") ?? NSNumber(value: 0)) as! T
            textField.resignFirstResponder()
        }

How does cast to any numeric work with NSNumber? Can I write my own type that can do this?

let d = 1.23
let I = d as! Int       // this will crash, so how does NSNumer do this?