0

I have an array called coordinateArray that is holding this data:

["(19.452187074041884", " -99.1457748413086)", "(19.443769985032485", " -99.14852142333984)", "(19.443446242121073", " -99.13787841796875)", "(19.450244707639662", " -99.13822174072266)"]
["(19.407780723677718", " -99.18417591514299)", "(19.400373302640162", " -99.18473381461808)", "(19.400049473260434", " -99.18039936485002)", "(19.405433052977592", " -99.17838234367082)"]
["(19.4022042123319", " -99.1457748413086)", "(19.401070819438004", " -99.16139602661133)", "(19.39184146912981", " -99.16268348693848)", "(19.389736456288546", " -99.14706230163574)"]
["(19.42114689571205", " -99.17375564575195)", "(19.425598915444077", " -99.15392875671387)", "(19.414913863184566", " -99.15470123291016)", "(19.41264724664359", " -99.16594505310059)", "(19.412890099927345", " -99.16766166687012)"]

I need to remove the parenthesis from each coordinate

How can I do this in Swift 3.0?

I tried to do this

let coordinateArray = coor.components(separatedBy: ",")                    
                    var coordinateArrayF = [String]()
                    for coordinate in coordinateArray {
                        let coordinatevar = coordinate.replacingOccurrences(of: "()", with: "")
                        coordinateArrayF.append(coordinatevar)
                    }

But it isn't working what am I doing wrong?

2
  • Better fix the code that generates the array, and make it an array of tuples or CLLocationCoordinate2D or something like that. Commented Feb 14, 2017 at 23:24
  • That was what I was trying to do since the beginning but I don't know how! I'm generating the array from a huge string of coordinates using components(separatedBy: ",") what would I need to do to make an array of CLLocationCoordinate2D? Thanks what I'm receiving Commented Feb 14, 2017 at 23:35

1 Answer 1

1

Functional programming is your friend!

var data = ["(19.452187074041884", " -99.1457748413086)", "(19.443769985032485", " -99.14852142333984)", "(19.443446242121073", " -99.13787841796875)", "(19.450244707639662", " -99.13822174072266)"]

let cleanData = data.map { 
     item in item.replacingOccurrences(of: "(", with: "")
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks I did something very similar but with a for loop, it's really nice to understand how to do it using functional programming. what was preventing it from working is that you need to create a regular ocurrence in the replacingOcurrences(of: "" with: "") method. I'll post the answer on how I solved it.

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.