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

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

问题描述

我正在尝试使用 NumberFormatter 使用Swift 3 Decimal ,但我对如何<$感到困惑c $ c>十进制正在实施中。我遇到的问题是 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)
    }
}

更一般地说,每次我需要传入一个对象类型时,我需要桥接十进制或写更多扩展名?

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

编辑

我想我更想知道 NSDecimal 十进制 NSDecimalNumber 。根本不清楚我在这里发生了什么。我应该用Swift 3的 Decimal 替换 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如何将标准字符串,数字和集合类型桥接到相应的Foundation类。

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.

一开始我认为意味着十进制是新的 NSDecimalNumber ,如错误是新 NSError - 但现在我不太确定。这也说'十进制值类型提供与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.

推荐答案

我在快速用户板上问了我的问题并得到了一个很好的答案,看看它这里: 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的方法。我想知道的是,我应该用Decimal替换我的应用程序中的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.

最终结果是十进制是很像 Int Double -it是一种值类型,可以方便地用于进行各种数学运算。 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是否计划使用NumberFormatter或其他面向对象的数字API来改进Decimal?希望我已经提供了足够的信息来回答我的问题。

Objective-C API使用 NSDecimalNumber 应该桥接到十进制,就像 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.

同样,你应该可以使用 Decimal 使用 NumberFormatter 转换为 NSDecimalNumber ,就像转换 Int Double NSNumber 将它们与 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天全站免登陆