Chintan Panchal Chintan Panchal - 4 months ago 55
Swift Question

Extra argument 'error' in call in swift

I am new to swift so please treat me as beginner.

I am following tutorial, this is pretty old tutorial and it has used GoogleMap framework whereas I am doing it with pod. In

func geocodeAddress
in
MapTasks.swift
file I am getting error called


Extra argument 'error' in call


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


So far I know is I am getting this error because of the diffrent version of swift. Tutorial I am following is written in old version of swift and I am doing it in new

enter image description here

Answer

In Swift 2.0, you cannot add 'error' argument in NSJSONSerialization method, you need to use try-catch statement as follows:

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

}

Comments