1

I'm looking for a quick way to map a a 2-dim array to JSON. I'm using SwiftyJSON and have hit a brick wall (I'm a hardware guy lol). Thank you!

import UIKit
import SwiftyJSON

var myPlaceArray = [Int:[String: Any]]()
myPlaceArray[0] = [
  "lat" : "2.590",
  "long" : "170.9"
  ]

myPlaceArray[1] = [
  "lat" : "42.90",
  "long" : "70.9"
]

//I can do this
let json = JSON(myPlaceArray[1]!)
print(json)

//but I cant do this
let jsonIndexed = JSON(myPlaceArray)
print(jsonIndexed[0] )
0

1 Answer 1

1

You are not declaring an array with this statement:

var myPlaceArray = [Int:[String: Any]]()

Instead, what you've declared above is a dictionary where the key is an Int and the values are dictionaries.

What you need is the following:

var myPlaceArray = [[String: Any]]()

Now, you can assign array elements as you did and you should be able to access the array as you wanted via your code. Give it a try and see :) If you run into any issues, comment here and I'll take a look.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help :) I know that dictionary and array notation can be a bit confusing at times. And if the above helped, please mark the answer as "correct" so that it helps others too.

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.