0

I have the following loop in ObjC: myArray is a 2D array.

for (NSArray *newArray in myArray) {

    for (int i = 0; i < newArray.count; i++) {

        NSArray *points = [newArray[i] componentsSeparatedByString:@","];
  }
}...

I am trying to do the same thing using Swift but am struggling to find a way to get componentsSeperatedByString:

for newArray in myArray {

        for i in 0..<newArray.count {
            var pnts:NSArray = newArray[i].componentsSeparatedByString(",")
        }
    }
2
  • It might help if you showed the declaration of myArray as well as some medication of what the actual problem is. Commented Nov 5, 2015 at 19:19
  • myArray is a 2d array of objects and their vectors. I am then looping through each object and pulling out each of it's vectors to test against so each element of newArray is a string vectory eg. "150.10 , 310.00" in swift I can't seem to find a method to separate the vectors. It will let me do newArray.componentsSeparatedByString(",") but not newArray[I].componentsSeperatedByString(",") Commented Nov 5, 2015 at 19:40

2 Answers 2

2

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]]
Sign up to request clarification or add additional context in comments.

Comments

0

If you want the pnts, you can use flatmap, which will flatten the 2D array into a single array with the points.

let pnts = myArray.flatMap { array in
            array.map { object in
            object.componentsSeparatedByString(",") 
        }
}

2 Comments

This doesn't seem to be doing the same thing as the original code. Note that points is declared local to the scope, so it doesn't seem to be accumulating.
Basically what I am trying to do is use a for loop to loop through an Array of Vectors stored as strings: "150.10, 310.20" , "400.10, 310.20" , etc.. I my swift loop above the newArray is only seen as an element and not an array so I cant do newArray[I].componentsSeperatedByString(",")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.