B.Chok
B.Chok

Reputation: 23

Ambiguous use go 'subscript'

I am trying to get data that has been encoded as json in a php script.

My code:

func getJson(completion: @escaping (Array<CarPark>) -> Void) {

        activityIndicatior.center = self.view.center
        activityIndicatior.hidesWhenStopped = true
        activityIndicatior.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        view.addSubview(activityIndicatior)
        self.activityIndicatior.startAnimating()

        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil
            {
                print("ERROR")
            }
            else
            {
                if let content = data
                {

                    do{

                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                        for index in 0..<myJson.count {

                            if let entry = myJson[index] as? NSDictionary{
                            let name = entry["Name"] as! String
                           let longitude = Double(entry["Longitude"] as! String)
                           let latitude = Double(entry["Latitude"] as! String)

                          let quiet = Int(entry["Quiet"] as! String)
                        let moderate = Int(entry["Moderate"] as! String)

                            let busy = Int(entry["Busy"] as! String)
                            let coordinate = CLLocationCoordinate2D( latitude: latitude!, longitude: longitude!)

                            print("coordinate lat is : \(coordinate.latitude)")
                            print("coordinate long is : \(coordinate.longitude)")
                            print("coordinate full is: \(coordinate)")
                            let tempPark = CarPark(name: name, latitude: latitude!, longitude: longitude!, quiet: quiet!, moderate: moderate!, busy: busy!, coordinate: coordinate, level: "Nil")
                            let level = tempPark.calcAvailability()
                            tempPark.level = level
                            print("Availability is \(tempPark.level)")
                            self.tempCarParks.append(tempPark)
                           // print("amount of parks: \(self.carParks.count)")
                           print("name of parks in array: \(self.tempCarParks[index].name)")
                            print("Availability is \(tempPark.level)")
                            }

                        }
                       completion(self.tempCarParks)

                    }
                    catch
                    {
                        print("Error")
                    }
                }
            }
    }

        task.resume()

    }

I am getting an error that says 'Ambiguous use of subscript' at the line:

if let entry = myJson[index] as? NSDictionary{

How can I fix this?

Upvotes: 0

Views: 44

Answers (1)

vadian
vadian

Reputation: 285150

Since you know myJson is an array why do you cast the object to unspecified AnyObject?

That causes the error, the compiler needs to know the concrete types of all subscripted objects. Help the compiler then the compiler helps you:

  let myJson = try JSONSerialization.jsonObject(with: content) as! [[String:Any]]

  for entry in myJson { // don't use old-fashioned index based loops

     let name = entry["Name"] as! String
 ...

.mutableContainers is completely useless in Swift.

Upvotes: 0

Related Questions