Swift 3 Decimal、NSDecimal 和 NSDecimalNumber [英] Swift 3 Decimal, NSDecimal and NSDecimalNumber

查看:45
本文介绍了Swift 3 Decimal、NSDecimal 和 NSDecimalNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 NumberFormatter 与 Swift 3 Decimal 一起使用,但我对 Decimal 的实际实现方式感到困惑.我遇到的问题是 Decimal 是一个结构体,所以每次我想使用我想要的格式化程序时,我都必须将它桥接到一个 NSDecimalNumber避免.

I am trying to use NumberFormatter with Swift 3 Decimal, but I'm confused as to how Decimal is really being implemented. The problem I'm having is that Decimal is a struct, so I have to bridge it to an NSDecimalNumber every time I want to use a formatter, which I'd like to avoid.

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let decimal = Decimal(integerLiteral: 3)
let string = formatter.string(from: decimal as NSDecimalNumber)

这是实现我自己的采用 Decimal 的扩展的理想解决方法吗?

Is the ideal workaround for this to implement my own extension that takes a Decimal?

extension NumberFormatter {
    open func string(from number: Decimal) -> String? {
        return string(from: number as NSDecimalNumber)
    }
}

更一般地说,每次我需要传入一个对象类型时,我是否需要桥接 Decimal 或编写更多扩展?

More generally, every time I need to pass in an object type am I going to need to bridge Decimal or write more extensions?

编辑

我想我更普遍地想知道 Swift 3 中的 NSDecimal DecimalNSDecimalNumber.我完全不清楚发生了什么在这.我应该用 Decimal 替换 Swift 3 的 NSDecimalNumber 吗?文档写道:

I guess I'm wondering more generally about NSDecimal Decimal and NSDecimalNumber in Swift 3. It's not clear to me at all what's going on here. Should I be replacing NSDecimalNumber with Decimal for Swift 3? The docs write:

Swift 覆盖到 Foundation 框架提供了 Decimal 结构,它连接到 NSDecimalNumber 类.Decimal 值类型提供与 NSDecimalNumber 引用类型相同的功能,并且两者可以在与 Objective-C API 交互的 Swift 代码中互换使用.这种行为类似于 Swift 将标准字符串、数字和集合类型桥接到它们相应的基础类的方式.

The Swift overlay to the Foundation framework provides the Decimal structure, which bridges to the NSDecimalNumber class. The Decimal value type offers the same functionality as the NSDecimalNumber reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes.

起初我认为 Decimal 是新的 NSDecimalNumber 就像 Error 是新的 NSError -但现在我不太确定了.这也说明Decimal 值类型提供与 NSDecimalNumber 引用类型相同的功能"——这是真的吗?我似乎无法获得很多相同的功能(当然,如果不先桥接它,这就是他们的意思吗?).我在这里和那里找到了一些帖子和一些信息,但没有什么令人信服的.有没有人有任何知识或见解?

Which at first I thought meant that Decimal was the new NSDecimalNumber like Error is the new NSError - but now I'm not so sure. That also says 'Decimal value type offers the same functionality as the NSDecimalNumber reference type` - is this really true? I can't seem to get much of the same functionality (without bridging it first, of course, is that what they mean?). I have found a few posts and a bit of info here and there, but nothing that convincing. Does anyone have any knowledge or insight?

我的应用专门使用 NSDecimalNumber 进行货币和格式设置,因此舍入和格式设置是重中之重.

My app specifically is using NSDecimalNumber for currency and formatting, so rounding and formatting are a high priority.

推荐答案

我在 swift-users 论坛上提出了我的问题并得到了很好的答案,请在此处查看:https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20161219/004220.html

I asked my question on the swift-users board and got a wonderful answer, check it out here: https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20161219/004220.html

回复的文本版本:

Swift 3 引入了 Decimal 类,起初我认为它应该替换"Swift 中的 NSDecimalNumber,例如 Error 是新的 NSError 等.但是,经过进一步调查,我不清楚这其实是这样的.看起来 Swift 3 的 Decimal 更像是一个 NSDecimal,但有一些桥接,因此它可以在需要时使用 NSDecimalNumber 的方法.我想知道的是,我应该用十进制替换我的应用程序中的 NSDecimalNumber 吗?Decimal 是 Swift 中 NSDecimalNumber 的继承者吗?

Decimal 实际上是一个结构体,而不是一个类,实际上它只是 Objective-C 中 NSDecimal 结构体的 Swift 名称.在 Swift 中,像 NSDecimalAdd 这样的函数被暴露为 Decimal 上的运算符,用于满足各种数字协议的要求,比如 SignedNumber.

Decimal is actually a struct, not a class, and in fact it's just the Swift name for the NSDecimal struct in Objective-C. In Swift, functions like NSDecimalAdd are exposed as operators on Decimal and are used to satisfy the requirements of various numeric protocols like SignedNumber.

最终结果是 Decimal 很像 IntDouble——它是一个值类型,可以方便地用来做所有事情各种数学.NSDecimalNumber 那么,等价于 NSNumber.

The end result is that Decimal is a lot like Int or Double—it's a value type which can be conveniently used to do all sorts of math. NSDecimalNumber, then, is equivalent to NSNumber.

由于我的应用程序进行了大量格式化,因此需要在 Decimal 和 NSDecimalNumber 之间进行大量转换,或者添加要使用的扩展名,所以我认为没有很多扩展名会很自然/方便.Swift 是否计划改进 Decimal 以使其更适用于 NumberFormatter 或其他面向对象的数字 API?希望我提供了足够的信息来回答我的问题.

使用NSDecimalNumber 的Objective-C API 应该桥接到Decimal,就像NSString 桥接到String>.但是,这种桥接可能不适用于类别方法;您可以通过在 Decimal 上编写一个扩展来将它们暴露给 Swift,该扩展将 self 转换为 NSDecimalNumber,然后调用您的 Objective-C 方法.

Objective-C APIs which use NSDecimalNumber should be bridged to Decimal, just as NSString is bridged to String. That bridging may not work for category methods, though; you could expose those to Swift by writing an extension on Decimal which casts self to NSDecimalNumber and then calls through to your Objective-C methods.

同样,您应该能够通过转换为 NSDecimalNumber 来使用 DecimalNumberFormatter,就像转换 IntDoubleNSNumber 以将它们与 NumberFormatter 一起使用.

Similarly, you should be able to use Decimal with NumberFormatter by casting to NSDecimalNumber, just as you would cast Int or Double to NSNumber to use them with NumberFormatter.

这篇关于Swift 3 Decimal、NSDecimal 和 NSDecimalNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆