繁体   English   中英

在 swift 中将 String 类型数组转换为 Float 类型数组无法将“String”类型的值分配给“Double”类型的下标

[英]Convert String type array to Float type array in swift Cannot assign value of type 'String' to subscript of type 'Double'

如何将具有字符串类型值的“numbersString”数组转换为具有浮点类型值的“numbersFloat”数组

问题是我不断收到“无法将'String'类型的值分配给'Double'类型的下标”作为错误

我知道我无法将浮点值输入到字符串下标,但我也不能更改字符串,因为它们是逗号分隔的并且不能将值放入数组中

var numbersString = [["564,00", "577,00", "13,00"], ["563,00", "577,00", "14,00"]] → I have
var numbersFloat = [[564.00, 577.00, 13.00], [563.00, 577.00, 14.00]] → I need

我尝试过的事情:

for row in 0...numbersString.count-1 {
    for col in 0...numbersString[0].count-1 {
        numbersFloat[row][col] = numbersString[row][col]
    }
}
Error: Cannot assign value of type 'String' to subscript of type 'Double'
for row in 0...numbersString.count-1 {
    for col in 0...numbersString[0].count-1 {
            var a = table[row][col]
            table[row][col] = Float(a)
    }
}

您可以使用NumberFormatter处理包含使用逗号作为小数分隔符的浮点数的字符串。 我通常将自定义格式化程序包装在 class 中。 看起来像这样:

class FloatFormatter {
    let formatter: NumberFormatter
    init() {
        formatter = NumberFormatter()
        formatter.decimalSeparator = ","
    }

    func float(from string: String) -> Float? {
        formatter.number(from: string)?.floatValue
    }
}

将其代入您的示例代码(修正浮点数组的类型),您将获得:

var numbersString = [["564,00", "577,00", "13,00"], ["563,00", "577,00", "14,00"]]
var numbersFloat: [[Float]] = [[564.00, 577.00, 13.00], [563.00, 577.00, 14.00]]

let floatFormatter = FloatFormatter()
for row in 0...numbersString.count-1 {
    for col in 0...numbersString[0].count-1 {
        numbersFloat[row][col] = floatFormatter.float(from: numbersString[row][col])!
    }
}

这行得通,但它不是很Swifty。 使用 map 会更好(这样您就不必担心匹配 arrays 的大小并预先分配浮点数组)。

let floatFormatter = FloatFormatter()
let numbersString = [["564,00", "577,00", "13,00"], ["563,00", "577,00", "14,00"]]
let numbersFloat = numbersString.map { (row: [String]) -> [Float] in
    return row.map { stringValue  in
        guard let floatValue = floatFormatter.float(from: stringValue) else {
            fatalError("Failed to convert \(stringValue) to float.")
        }
        return floatValue
    }
}

也许你想要这样的东西?

var numbersString = [["564.00", "577.00", "13.00"], ["563.00", "577.00", "14.00"]]

var numbersFloat: [[Float]] = Array(repeating: Array(repeating: 0.0, count: numbersString[0].count), count: numbersString.count)

print(numbersFloat.count)
for row in 0...numbersString.count - 1 {
    for col in 0...numbersString[0].count - 1 {

        print(row, col, numbersString[row][col])
        print(Float(numbersString[row][col]) ?? 0)
        numbersFloat[row][col] =  Float(numbersString[row][col]) ?? 0
    }
}

这将适用于两个“。” 和“,”分隔符。

var numbersString = [["564,00", "577,00", "13,00"], ["563,00", "577,00", "14,00"]]

let numberFormatter = NumberFormatter()
let decimalSeparator = ","
numberFormatter.decimalSeparator = decimalSeparator

let numbersFloat = numbersString.map({ $0.compactMap({ numberFormatter.number(from: $0.replacingOccurrences(of: ".", with: decimalSeparator))?.floatValue })})

对于所有人:NumberFormatter 具有.locale属性,并且在创建格式化程序时,区域设置设置为当前设备区域设置。 但小数分隔符在不同国家/地区有所不同。 当一切都对您甚至 QA 都很好,但对生产中的用户不起作用时,它可能会导致非常不愉快的错误。 因此,以所述方式控制 String-to-Double 转换始终是最佳选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM