2016-07-21 48 views
1

我是新來的快,所以請把我當初學者。
我正在關注tutorial,這是一個非常古老的教程,它使用了GoogleMap框架,而我正在使用pod。在func geocodeAddressMapTasks.swift文件我收到錯誤所謂在快速調用中額外的參數'錯誤'

額外的參數「錯誤」呼叫

func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) { 
    if let lookupAddress = address { 
     var geocodeURLString = baseURLGeocode + "address=" + lookupAddress 
     geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 

     let geocodeURL = NSURL(string: geocodeURLString) 

     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      let geocodingResultsData = NSData(contentsOfURL: geocodeURL!) 

      let request = NSMutableURLRequest(URL: geocodingResultsData) 

      let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
       (let data, let response, let error) in 

       if let _ = response as? NSHTTPURLResponse { 
        do { 
         let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary 

         if error != nil { 
          print("error=\(error!)") 
          return 
         } 

         if let parseJSON = json { 


         } 
        } catch { 
         print(error) 
        } 
       } 
      } 
      task.resume() 
      else { 
       // Get the response status. 
       let status = dictionary["status"] as! String 

       if status == "OK" { 
        let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>> 
        self.lookupAddressResults = allResults[0] 

        // Keep the most important values. 
        self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String 
        let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject> 
        self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue 
        self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue 

        completionHandler(status: status, success: true) 
       } 
       else { 
        completionHandler(status: status, success: false) 
       } 
      } 
     }) 
    } 
    else { 
     completionHandler(status: "No valid address.", success: false) 
    } 
} 

到目前爲止,我所知道的是我得到的,因爲迅速的有多種不同的版本錯誤。教程我下面寫在舊版本的迅速和我在新

enter image description here

+0

你是不是有關聯的方法變量 '錯誤'。你只是在方法中聲明變量。它不會工作。嘗試刪除「var error?」行。如果它不起作用,並且你在使用Swift 2.0進行操作,那麼你需要在NSJSONSerialization上使用do-try-catch方法。 –

回答

2

做在雨燕2.0,則不能添加在NSJSONSerialization方法「錯誤」的說法,你需要使用try-catch語句如下:

func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) { 

if let lookupAddress = address { 
    var geocodeURLString = baseURLGeocode + "address=" + lookupAddress 
    geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 

    let geocodeURL = NSURL(string: geocodeURLString) 

    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     let geocodingResultsData = NSData(contentsOfURL: geocodeURL!) 

     let request = NSMutableURLRequest(URL: geocodeURL!) 

     let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
      (let data, let response, let error) in 

      if let _ = response as? NSHTTPURLResponse { 
       do { 
        let dictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary 

        if error != nil { 
         print("error=\(error!)") 
         return 
        } 

        if let parseJSON = dictionary { 
         let status = dictionary["status"] as! String 

         if status == "OK" { 
          let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>> 
          self.lookupAddressResults = allResults[0] 

          // Keep the most important values. 
          self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String 
          let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject> 
          self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue 
          self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue 

          completionHandler(status: status, success: true) 
         } 
         else { 
          completionHandler(status: status, success: false) 
         } 

        } 
       } catch { 
        print(error) 
       } 
      } 
     } 
      task.resume() 
     }) 
     } 

}

+0

我無法按照你的建議。我的代碼的哪一部分應該使用代碼進行更改以使其正常工作?對不起,但我是Swift初學者。 – ctpanchal

+0

刪除「var error?」並刪除「let dictionary ...」方法,並用我給出的代碼替換它。 –

+0

當我正在嘗試,它給了我很多其他的錯誤 – ctpanchal

相關問題