# With only \<T: Numeric\> type, how to create an NSNumber?

**URL:** <https://forums.swift.org/t/with-only-t-numeric-type-how-to-create-an-nsnumber/47663>\
**Category:** Using Swift\
**Created:** [April 23, 2021, 10:32pm UTC](https://forums.swift.org/t/with-only-t-numeric-type-how-to-create-an-nsnumber/47663 "2021-04-23T22:32:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 23, 2021, 10:32pm UTC](https://forums.swift.org/t/with-only-t-numeric-type-how-to-create-an-nsnumber/47663/1 "2021-04-23T22:32:43Z")

</div>

```swift
struct NumberField<T: Numeric & CustomStringConvertible>: View {
    @Binding var value: T
    let formatter: NumberFormatter

    var body: some View {
        // how can I create an NSNumber from `value`?
        // this doesn't work:
        let n1 = NSNumber(value: value) // No exact matches in call to initializer 

        // This work, but seem not ideal
        let n2 = formatter.number(from: value.description) ?? NSNumber(value: 0)
    }
}
```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [April 24, 2021, 12:29am UTC](https://forums.swift.org/t/with-only-t-numeric-type-how-to-create-an-nsnumber/47663/2 "2021-04-24T00:29:48Z")

</div>

You can't; `Numeric` is a _very_ general protocol, and lots of things that can conform to `Numeric` (like say `Complex<Double>` from Swift Numerics or a type that represents polynomials with integer coefficients) have no sensible representation as NSNumbers.

Given that you _want_ to do this, it's safe to say that your constraint should be significantly more restrictive than `Numeric`; what are the actual requirements for the types you need to represent?

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 24, 2021, 1:03am UTC](https://forums.swift.org/t/with-only-t-numeric-type-how-to-create-an-nsnumber/47663/3 "2021-04-24T01:03:51Z")

</div>

Edit: [solution to this question](https://forums.swift.org/t/binding-t-how-to-determine-wrappedvalue-is-signed-numeric-value/47668/4).

> [@scanon](#):
>
> what are the actual requirements for the types you need to represent?

All the numeric types `NSNumber` support except `Bool` and `CChar`, exactly these I copy from all the `init(value:)`'s:

Double  
Float  
Int32  
Int  
Int64  
Int16  
UInt8  
UInt32  
UInt  
UInt64  
UInt16

[this is my original question about casting an NSNumber to a \<T: that-NSNumber-Support-Except-Bool-And-CChar\>](https://forums.swift.org/t/is-a-giant-switch-case-the-right-way-to-convert-nsnumber-to-a-generic-type-t/47655/5)
