1

I am using the following source code to retrieve some data from Google Maps:

import UIKit
import GoogleMaps
import GooglePlaces
import SwiftyJSON

class Place /*: NSObject*/ {
    let name: String

    init(diction:[String : Any])
    {
        let json = JSON(diction)
        name = json["name"].stringValue //as! String       
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&location=54.507514,-0.073603&radius=1000&name=Specsavers&name=Opticians&key=AIzaSyAcXLd8jotAyIOgKYqhYbL703BIibXkd-M"

       guard let url = URL(string: urlString) else {return}

        URLSession.shared.dataTask(with: url) {(data, response, error) in

            if let content = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if let results = json["results"] as? [[String : Any]] {
                        var places = [Place]()
                        for place in results {
                            print("HERE0: " place)
                            places.append(Place(diction: place))
                        }
                        print("HERE1:", places)
                    }
                    else {
                        print("return")
                    }

                }
                catch{

                }
            }
        }.resume()

    }

But when I try to print places then I get the following response:

2017-11-13 11:16:56.887909+0000 Trial[3911:127944] [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1757 received sct extension length is less than sct data length

and I get this output from HERE1:

HERE1: [Trial.Place] (My xcode project is named Trial)

while from HERE0 I get properly the json file as it is.

Why I cannot retrieve properly the name of the store from Google Places?

  • dataTask(with: url) but there is no variable with named of url. try to change it with urlString – Alper Nov 13 '17 at 11:41
  • I apologise but for some reason I did not copy and paste my source code accurately. So in my original source code I have the url. –  Nov 13 '17 at 11:51

1 Answers1

1

Your mistake is, places actually an Array of Place class. So when you print it out you should specify an index. Because of it is an array of class, after index you should type which property you want to see.

I refactored your code, you can use it.

Your class file

class Place /*: NSObject*/ {
let name: String

init(diction:[String : Any])
{
    self.name = diction["name"] as! String
}
}

Your json parse

 var places = [Place]() //do it outside of the viewDidload


 let urlString = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&location=51.507514,-0.073603&radius=1000&name=Specsavers&name=Opticians&key=AIzaSyAcXLd8jotAyIOgKYqhYbL703BIibXkd-M")

    let session = URLSession.shared
    let task = session.dataTask(with: urlString!) { (data, response, err) in
        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableLeaves) as! Dictionary<String,Any>
                if let results = json["results"] as? [[String:Any]] {
                    for place in results {
                        self.places.append(Place(diction: place))
                        print(self.places[0].name)
                    }
                    print(self.places[0].name)
                }
            }catch let err{
                print(err)
            }

        }
    }
    task.resume()
Alper
  • 1,415
  • 2
  • 13
  • 20
  • This works and thank you for this. But actually I feel that it is possible to write something something closer to my source code to fix this mistake (or not?). –  Nov 13 '17 at 12:14
  • By this I mean that if I add the following line " print(places[0].name)" then I have the same result. –  Nov 13 '17 at 12:31
  • I see there is only one response in api. In this case using array is wrong. – Alper Nov 13 '17 at 15:15
  • Both options `.mutableContainers` and `.mutableLeaves` are useless in Swift. Don't suggest that option. Omit the `options` parameter. – vadian Nov 13 '17 at 16:41