Veronica G Veronica G - 5 months ago 11
Swift Question

How to turn an array of an array of doubles into string, Swift

I have an array of array of doubles. How do you convert that to a single string printed on the console?

I need to create a function that takes in an array of arrays of doubles as a parameter and returns a string object. Then loop through each of the inner arrays and concatenate each element of the inner arrays into a String object. But I'm not sure how to go about that.

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]


This is the code I have... but i don't know how to do a for loop that would give me my answer...

func nestedFunction(nestedArray: [[Double]]) -> String {
var stringVar: String = ""

( for loop here... )

return stringVar
}

print(nestedArrayFunction(nestedArray))


The expected output should be a string object, with no brackets

Answer

From the description in your question it seems as if the argument of nestedFunction(...) should be an array of arrays with double valued elements ([[Double]]) rather than an array with double valued elements ([Double]).

You can make use of .flatten() to your simply nested array nestedArray in nestedFunction(...), and thereafter e.g. reduce to transform the Double valued elements of the flattened array to one concenated String.

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    return String(nestedArray
        .flatten()
        .reduce("") { $0 + ", " + String($1) }
        .characters.dropFirst(2))
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

As an alternative, if you're set on using a for loop, you can use for ... in on the flattened array, e.g.:

var array1 = [2.6, 6.7, 7.2, 4.1, 3.1]
var array2 = [1.2, 3.5, 2.8, 4.5, 6.4]
var array3 = [1.2, 1.3, 1.4, 1.5, 1.6]

var nestedArray = [array1, array2, array3]

func nestedFunction (nestedArray: [[Double]])-> String {
    var stringVar: String = ""
    var isFirstElement = true
    for elem in nestedArray.flatten() {
        stringVar += (isFirstElement ? "" : ", ") + String(elem)
        isFirstElement = false
    }
    return stringVar
}

let asString = nestedFunction(nestedArray)
    // asString = "2.6, 6.7, 7.2, 4.1, 3.1, 1.2, 3.5, 2.8, 4.5, 6.4, 1.2, 1.3, 1.4, 1.5, 1.6"

Note that due to limited floating point precision, some double values might end up with a "messy" String representation (e.g. 2.6 might end up as 2.6000000000000001) when using the direct String initializer (String($1) and String(elem) above, in the first and second method, respectively). To redeem this you could set a fixed number of fraction digits for the String representation of your Double values, using the following String initializer:

String(format: "%.3f", myDoubleValue)
    /*             \
           number of fraction digits (3 here) */

E.g., replace String($1) and String(elem) in the methods above by String(format: "%.3f", $1) and String(format: "%.3f", elem), respectively, for some number of fraction digits of your choice. The Double values will be rounded to the number of supplied fraction digits.