模糊使用去下标'

时间:2017-03-02 17:28:50

标签: json swift xcode subscript

我试图在php脚本中获取已编码为json的数据。

我的代码:

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()

    }

我收到的错误是“对下标的不明确使用”。在这一行:

如果让entry = myJson [index]为?的NSDictionary {

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

既然您知道myJson是一个数组,为什么要将对象转换为未指定的AnyObject

导致错误,编译器需要知道所有下标对象的具体类型。帮助编译器然后编译器帮助你:

  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在Swift中完全没用。