Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Fetch mySql data to iOS in swift 3 - again

dickthedevdickthedev Posts: 214Registered Users @ @
edited June 11 in iOS SDK Development
Hello, I have asked this seemingly very straight forward question a few times now, never got a solution.

I have a url to obtain mySql data
// 1. get url
let url = URL(string:"")

// 2. Fetch data from url
let data = try? Data(contentsOf: url!)
playground Output is: 102 bytes. So obviously xcode gets the response data from the URL.

Then I move on to extracting the data:
let urlDict  = try? JSONSerialization.jsonObject(with: data!, options: [])
// playground Output is: [["Latitude": "37.331741", "Address": "1 Infinite Loop Cupertino, CA", "Name": "Apple", "Longitude": "-122"]]

// playground Output is:
"(\n {\n Address = "1 Infinite Loop Cupertino, CA";\n Latitude = "37.331741";\n Longitude = "-122";\n Name = Apple;\n }\n)\n"

My understanding is urlDict is of a Any type. Am I correct?

My biggest question is how can I cast or convet urlDict so that I can access the value using key=>value ????? Like this urlDict!["Address"] Outputs "1 Infinite Loop Cupertino, CA"
urlDict!["Latitude"] Outputs "37.331741"...

I am a newbie to Swift so I am doing this as an exercise, any help will be greatly appreciated.
Post edited by dickthedev on


  • ryantcbryantcb Posts: 666Registered Users @ @ @
    Likelihood but not always your Json will be an array of objects (dictionaries) so cast it as from the off and then loop through each item in the array of dictionaries

    let urlDict = try? JSONSerialization.jsonObject(with: data!, options: []) as! [NSDictionary]
Sign In or Register to comment.