单击特定地址时,在搜索栏中发生了Google Maps错误



我在搜索栏中显示地址

错误:由于未知的例外" nsrangeException",终止应用程序, 原因:'*** - [__ __ nssingleobjectArrayi objectatexex:]:索引1 边界[0 .. 0]'

 override func tableView(_ tableView: UITableView,
                            didSelectRowAt indexPath: IndexPath)
    {
        self.dismiss(animated: true, completion: nil)
            let urlpath = "https://maps.googleapis.com/maps/api/geocode/json?address=(self.searchResults[indexPath.row])&sensor=false".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            let url = URL(string: urlpath!)
            let task = URLSession.shared.dataTask(with: url!) { (data, response, error) -> Void in
                do {
                    if data != nil{
                        let dic = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
                        let status = dic.value(forKey: "status") as! String
                        print("(status)")
                        if status == "OK"
                        {
                        let lat =   (((((dic.value(forKey: "results") as! NSArray).object(at: indexPath.row) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lat")) as? Double
                        let lon =   (((((dic.value(forKey: "results") as! NSArray).object(at: indexPath.row) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lng")) as? Double
                        if let latitude = lat
                        {
                            if let longitude = lon
                            {
                                self.delegate.locateWithLongitude(longitude, andLatitude: latitude, andTitle: self.searchResults[indexPath.row])
                            }
                        }
                    }
                    }
                }
                catch
                {
                    print("Error")
                }
            }
            task.resume()
    }

尝试此

override func tableView(_ tableView: UITableView,
                        didSelectRowAt indexPath: IndexPath)
{
    self.dismiss(animated: true, completion: nil)
        let urlpath = "https://maps.googleapis.com/maps/api/geocode/json?address=(self.searchResults[indexPath.row])&sensor=false".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        let url = URL(string: urlpath!)
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) -> Void in
            do {
                if data != nil{
                    let dic = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
                    let status = dic.value(forKey: "status") as! String
                    print("(status)")
                    if status == "OK"
                    {
                    let lat =   (((((dic.value(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lat")) as? Double
                    let lon =   (((((dic.value(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "geometry") as! NSDictionary).value(forKey: "location") as! NSDictionary).value(forKey: "lng")) as? Double
                    if let latitude = lat
                    {
                        if let longitude = lon
                        {
                            self.delegate.locateWithLongitude(longitude, andLatitude: latitude, andTitle: self.searchResults[indexPath.row])
                        }
                    }
                }
                }
            }
            catch
            {
                print("Error")
            }
        }
        task.resume()
}

相关内容

最新更新