ios - 在 Swift 中将整数映射到字符串

标签 ios swift dictionary higher-order-functions swift5

Integer 映射到 String 的最简单方法是什么? 这是一个整数数组。

let englishIntLiterals: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

我想将每个数字的出现映射到它们的尼泊尔等效数字,它表示为一个字符串数组。

let nepaliLiterals: [String] = ["१", "२", "३", "४", "५", "६", "७", "८", "९", "०"]

我需要 1 替换为 等等。 我知道有一个解决函数的方法,但我正在寻找一个高阶函数解决方案,我一直无法弄清楚。

最佳答案

您可以使用固定区域数字格式化程序来显示本地化的整数:

extension Formatter {
    static let numberNepal: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.locale = Locale(identifier: "ne_NP")
        formatter.numberStyle = .decimal
        return formatter
    }()
}
extension BinaryInteger {
    var nepaliFormatted: String {
        return Formatter.numberNepal.string(for: self)!
    }
}

1234567890.nepaliFormatted  // "१,२३४,५६७,८९०"

let englishIntLiterals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

let nepaliLiterals = englishIntLiterals.map{$0.nepaliFormatted}  // "१", "२", "३", "४", "५", "६", "७", "८", "९", "०"]

关于ios - 在 Swift 中将整数映射到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550055/

相关文章:

ios - 添加配件类型在 UITableViewCell 中移动标签

ios - NSMetadataQuery 什么都不返回

iphone - iPhone 应用程序中的 map 集成

c# - 如何将 IDictionary<TKey, List<TValue>> 转换为 IDictionary<TKey, IEnumerable<TValue>>?

ios - performSegue 动画完成,什么标识符?

ios - Mac OSX 在构建 MonoDevelop iOS 应用程序时要求访问钥匙串(keychain)

ios - 无法将类型 '(Void) -> ()' 的值转换为预期的参数类型 '() -> Void'

ios - PhoneGap Your Binary 未针对 iPhone 5 进行优化

swift - macOS Swift 主从委托(delegate)/协议(protocol)不起作用

Android - Google maps Api v2 - 身份验证错误