Advertise here




Advertise here

Howdy, Stranger!

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

Badges

dickthedev @ @

About

Username
dickthedev
Joined
Visits
303
Last Active
Roles
Registered Users
Points
62
Posts
214
Badges
11
  • Fetch mySql data to iOS in swift 3 - again

    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:"http://www.mobwebplanet.com/phpWebService/sample.php")
    
    // 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"]]
    

    print(urlDict!)
    // 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.