[swift-users] Replacement for NSNumber?

Jens Alfke jens at mooseyard.com
Mon Apr 11 10:58:32 CDT 2016


Or just create an enum with an Int and a Double field. I believe this will be more efficient since the values can be stored inline, whereas indirecting through a protocol will force it to be boxed into a heap object (right?)

—Jens

> 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 <mailto:SlaunchaMan at gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>
>> On Apr 11, 2016, at 11:04 AM, Maury Markowitz via swift-users <swift-users at swift.org <mailto: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 <mailto: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160411/3beb3f41/attachment.html>


More information about the swift-users mailing list