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.