3

So im getting this array in the form of a string from the server with all the coordinates of objects, shown below:

"[[[-0.340254,51.605946],[-0.340278,51.605685],[-0.339718,51.604400],
 [-0.339280,51.603746],[-0.338915,51.603454],[-0.338657,51.603018],
 [-0.338427,51.601810],[-0.338518,51.600885],[-0.337471,51.599908],
 [-0.337378,51.599682],[-0.337456,51.599116],[-0.336860,51.597669],
 [-0.335843,51.597043],[-0.335635,51.596816],[-0.335112,51.595720],
 [-0.335232,51.594400],[-0.335057,51.593273],[-0.334827,51.592847],
 [-0.333187,51.591889],[-0.333236,51.590945],[-0.332894,51.590446],
 [-0.332727,51.589868],[-0.332791,51.589320],[-0.332638,51.589156],
 [-0.332028,51.587295],[-0.332326,51.585438],[-0.332243,51.585365],
 [-0.332292,51.585186],[-0.331651,51.582991],[-0.333713,51.581096],
 [-0.334020,51.580570],[-0.334055,51.580013],[-0.337963,51.580123],
 [-0.340047,51.579954],[-0.341778,51.579979],[-0.341883,51.579881]]]"

how would i convert this into an array? Thank you in advance!

so i would want it the form [[Double]]

let objects = [[[-0.340254,51.605946],[-0.340278,51.605685],[-0.339718,51.604400],
 [-0.339280,51.603746],[-0.338915,51.603454],[-0.338657,51.603018],
 [-0.338427,51.601810],[-0.338518,51.600885],[-0.337471,51.599908],
 [-0.337378,51.599682],[-0.337456,51.599116],[-0.336860,51.597669],
 [-0.335843,51.597043],[-0.335635,51.596816],[-0.335112,51.595720],
 [-0.335232,51.594400],[-0.335057,51.593273],[-0.334827,51.592847],
 [-0.333187,51.591889],[-0.333236,51.590945],[-0.332894,51.590446],
 [-0.332727,51.589868],[-0.332791,51.589320],[-0.332638,51.589156],
 [-0.332028,51.587295],[-0.332326,51.585438],[-0.332243,51.585365],
 [-0.332292,51.585186],[-0.331651,51.582991],[-0.333713,51.581096],
 [-0.334020,51.580570],[-0.334055,51.580013],[-0.337963,51.580123],
 [-0.340047,51.579954],[-0.341778,51.579979],[-0.341883,51.579881]]]

so if i was to do objects[0][0] it should return [-0.340254,51.605946]

func convert(s: String) -> [[[Double]]]{

do{

    let array = try NSJSONSerialization.JSONObjectWithData(s.dataUsingEncoding(NSUTF8StringEncoding)!, options: []) as? [[[Double]]]
    return array!

}catch{

}
return [[[]]]

}

3
  • This IS an array. Show us an example of what you want it to look like Commented Aug 7, 2016 at 15:51
  • 1
    its a string the speech marks Commented Aug 7, 2016 at 15:54
  • Please don't include the answer in your question. If an answer solved your issue, mark the answer as accepted; you can also post your own answer if you resolved your problem yourself. Thank you. Commented Aug 8, 2016 at 10:55

1 Answer 1

3

You simply have a JSON response of a 3D array. I loaded your string into the Swift REPL, and was able to parse it like so:

import Foundation

let s = /* your string */
let array = NSJSONSerialization.JSONObjectWithData(s.dataUsingEncoding(NSUTF8StringEncoding)!, options: []) as? [[[Double]]] 

Output

$R3: [[[Double]]]? = 1 value {
  [0] = 36 values {
...

Code Sample

func convert(s: String) -> [[[Double]]] {

    if let data = s.dataUsingEncoding(NSUTF8StringEncoding),
        let object = try? NSJSONSerialization.JSONObjectWithData(data, options: []),
        let array = object as? [[[Double]]]
    {
        return array
    }

    return [[[]]]
}
Sign up to request clarification or add additional context in comments.

4 Comments

im new to swift, so i have never used guard? or try? any help would be appreciated! :)
@MinhalKhan you need to post more of your code so I can use that context to demostrate
@MinhalKhan The idea when writing Swift code is to avoid unconditional unwrapping, aka screaming, using !. Every time your code screams, it is a potential crash. Sometimes a crash is desirable (i.e. unrecoverable error), but for parsing data from the network, probably it isn't.
Yeah, been looking at more material! but thanks! I was stuck on this for ages!

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.