[swift-evolution] [swift-users] Replacement for NSNumber?

Kenny Leung kenny_leung at pobox.com
Mon Apr 11 10:15:13 CDT 2016


Moving this to swift-evolution.

It’s inconvenient that structs can’t inherit, and so things like Int and Double aren’t known as numbers. Should there be a Number protocol that encompasses all things number-like?

-Kenny


> On Apr 11, 2016, at 8:06 AM, Jeff Kelley via swift-users <swift-users at swift.org> wrote:
> 
> One possible solution is to create an empty Protocol that both Int and Double conform to, perhaps DataValue, and then create your data structure like this:
> 
> var data: [String: DataValue]
> 
> 
> Jeff Kelley
> 
> SlaunchaMan at gmail.com | @SlaunchaMan | jeffkelley.org
> 
>> On Apr 11, 2016, at 11:04 AM, Maury Markowitz via swift-users <swift-users at swift.org> wrote:
>> 
>> I have a primary data structure that is currently defined such:
>> 
>> var data: [String: NSNumber]
>> 
>> This is the only place in my app where I use a Foundation type *as* a Foundation type (as opposed to a bridge), and I would strongly prefer this to be purely Swift types.
>> 
>> I used NSNumber because the original data is variant, a mix of Int and Double - in some instances the first two entries in the dict are Ints, while in others it's the first four, and others are a mix based on other data points (it's very annoying). I could use two dictionaries, but this would *greatly* confuse code further into the program.
>> 
>> I know I could use Double and then floor the ints. But I'm wondering if I'm missing some more natural solution? I don't believe there is a common base type for Int and Double, but perhaps there is another way to solve this?
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-evolution mailing list