快速将String类型数组转换为Float类型数组无法将'String'类型的值分配给'Double'类型的下标

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

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

我知道我无法将Float值输入到String下标,但是我既不能更改String,也不能更改,因为它们之间用逗号分隔并且无法将值放入数组中

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)
    }
}
评论
  • 俄对花儿笑
    俄对花儿笑 回复

    也许您想要这样的东西?

    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
        }
    }