0

I have a variable which contains a location in String format. This location is an ex CLLocation converted in String to upload it in my database. Then, this location looks like this:

"<+48.77516697,+2.27782000> +/- 65.00m (speed..."; (like a real CLLocation value but in String format).

I want to convert it back in CLLocation format.

I don't have any code at the moment because I don't know at all how to do this. Thanks in advance for your help.

Grimxn
  • 22,115
  • 10
  • 72
  • 85
Paul Bénéteau
  • 775
  • 2
  • 12
  • 36
  • A better question might be, why not convert it into a more useful string format to start with instead of using the debugging string. – David Berry Mar 01 '17 at 20:22

1 Answers1

2

A very simple way is as follows:

let s = "<+48.77516697,+2.27782000> +/- 65.00m (speed..."
let ss = s.components(separatedBy: CharacterSet(charactersIn: "<,>")).flatMap({
    Double($0)
}) // [48.77516697, 2.27782]
let c = CLLocationCoordinate2D(latitude: ss[0], longitude: ss[1])

More sophisticated solutions should probably use Scanner (NSScanner in Obj-C) as explained in this answer. Alternatively, if you're feeling masochistic, check out RegularExpression/NSRegularExpression... :)

Community
  • 1
  • 1
Grimxn
  • 22,115
  • 10
  • 72
  • 85