2016-11-03 104 views
0

因此,如果我上傳錯誤,請耐心等待。我有這兩條線的麻煩:Swift 3編譯器錯誤:無法調用非函數類型的值'任何?!'

let lat = ((((dic["results"] as AnyObject).value(forKey: "geometry") as AnyObject).value(forKey: "location") as AnyObject).value(forKey: "lat") as AnyObject).object(0) as! Double 
let lon = ((((dic["results"] as AnyObject).value(forKey: "geometry") as AnyObject).value(forKey: "location") as AnyObject).value(forKey: "lng") as AnyObject).object(0) as! Double 

這兩條線彈出一個錯誤說:

Cannot call value of non-function type 'Any?!'

不知道如何解決這個問題?

這裏是所有的代碼從視圖控制器....

import UIKit 

protocol LocateOnTheMap { 
    func locateWithLongitude(_ lon: Double, andLatitude lat: Double, andTitle title: String) 
} 

class SearchResultsController: UITableViewController { 

    var searchResults: [String]! 
    var delegate: LocateOnTheMap! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.searchResults = Array() 
     self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier") 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 

     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return self.searchResults.count 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) 

     cell.textLabel?.text = self.searchResults[indexPath.row] 
     return cell 
    } 



    override func tableView(_ tableView: UITableView, 
          didSelectRowAt indexPath: IndexPath){ 
     // 1 
     self.dismiss(animated: true, completion: nil) 
     // 2 
     let correctedAddress:String! = self.searchResults[indexPath.row].addingPercentEncoding(withAllowedCharacters: CharacterSet.symbols) 
     let url = URL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)&sensor=false") 

     let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in 
      // 3 
      do { 
       if data != nil{ 
        let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSDictionary 

        let lat = ((((dic["results"] as AnyObject).value(forKey: "geometry") as AnyObject).value(forKey: "location") as AnyObject).value(forKey: "lat") as AnyObject).object(0) as! Double 
        let lon = ((((dic["results"] as AnyObject).value(forKey: "geometry") as AnyObject).value(forKey: "location") as AnyObject).value(forKey: "lng") as AnyObject).object(0) as! Double 
        // 4 
        self.delegate.locateWithLongitude(lon, andLatitude: lat, andTitle: self.searchResults[indexPath.row]) 
       } 

      } catch { 
       print("Error") 
      } 
     }) 
     // 5 
     task.resume() 
    } 


    func reloadDataWithArray(_ array:[String]){ 
     self.searchResults = array 
     self.tableView.reloadData() 
    } 
} 
+0

嘗試更換'AnyObject'在'讓LON =((((DIC [ 「成果」]鑄。 ..'與這樣的字典一致'[String:Any]' –

回答

1

在Swift 3中,您應該將基於中間JSON的對象向右在潛入鏈條之前鍵入。

你可以是這樣的:

let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [String:Any] 

然後:

let lon = (((dic["results"] as! [String:Any])["geometry"] as! [String:Any]).["location"] as! [String:Any])["lng"] as! Double 
+0

@LBIMBA:這對你有幫助嗎? –

相關問題