The first step is to convert your NSArray to have the proper type, in this case it looks like you want:
if let array = myArray as? [[String]] {
}
Then you have a number of options, the most effective of which is probably a nested iteration:
let myArray = NSArray(arrayLiteral:
NSArray(arrayLiteral: "150.10,310.20"),
NSArray(arrayLiteral: "400.10,310.20", "100,200")
)
if let source = myArray as? [[String]] {
for array in source {
for element in array {
print(element.componentsSeparatedByString(","))
}
}
}
// -> ["150.10", "310.20"]
// ["400.10", "310.20"]
// ["100", "200"]
If all you're wanting to do is extract the points, you can use map:
if let source = myArray as? [[String]] {
print(source.map {
$0.map { $0.componentsSeparatedByString(",") }
}
)
}
// -> [[["150.10", "310.20"]], [["400.10", "310.20"], ["100", "200"]]]
If you want to collapse the points into a single array, you can use flatMap, as pointed out by Helium, which will turn the array of arrays, into a simple array:
if let source = myArray as? [[String]] {
print(source.flatMap {
$0.map { $0.componentsSeparatedByString(",") }
}
)
}
// -> [["150.10", "310.20"], ["400.10", "310.20"], ["100", "200"]]
Of course, you can also extract it as [[Double]] instead of [[String]] by using yet another map call:
if let source = myArray as? [[String]] {
print(source.map {
$0.map { $0.componentsSeparatedByString(",").map { Double($0)! } }
})
}
// -> [[150.1, 310.2], [400.1, 310.2], [100.0, 200.0]]