-1

I'm getting the error "extra argument in call" from my httpGet(request) and I'm not sure why. I've looked at several stack exchange solutions and it seems like I'm doing everything that the solutions are suggesting. Extra argument 'error' in call. Compilation error in Swift 2.0. With this solution in particular- it looks like I am doing the error handling correctly.

    class func userNameCall() -> String {

        let request = NSMutableURLRequest(URL: NSURL(string: "my url goes here")!)

        httpGet(request) { string, error in
            guard error == nil && string != nil else {
                print(error?.localizedDescription)
                return
            }
            let data = string!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
            do {
                let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]

                //if JSON returns error with key "msg" meaning log in was not successful
                if let message = json["msg"] as! String?{
                    if (message == "Invalid login."){
                        self.signInFailedAlert()
                        return "Invalid login"
                    }
                }

                //if JSON has key "value", meaning a session ID was created and login was successful, continue to second call
                if let sessionID = json["value"] as! String?{
                    print("sessionID from httpCall class: " + sessionID)
                    return sessionID
                }
            } catch let error as NSError { //
                print("Failed to load: \(error.localizedDescription)")
            }
        }
    }

func httpGet(request: NSURLRequest, callback: (String?, NSError?) -> Void) {
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {
                callback(nil, error)
                return
            }

            callback(String(data: data!, encoding: NSUTF8StringEncoding), nil)
        }
        task.resume()
    }

Can anyone tell me what I'm doing wrong? Thank you!

Community
  • 1
  • 1
Tessa
  • 176
  • 13
  • on which line do you get that error? try to clean and rebuild your project. – Sulthan Feb 11 '16 at 21:59
  • on the `httpGet(request) {string, error in` line it would probably also help to have the function for that: `func httpGet(request: NSURLRequest, callback: (String?, NSError?) -> Void) { let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in guard error == nil && data != nil else { callback(nil, error) return } callback(String(data: data!, encoding: NSUTF8StringEncoding), nil) } task.resume() }` – Tessa Feb 11 '16 at 22:01
  • `guard error == nil && data != nil else {` change to `guard let data = data where error == nil else { return }` – Leo Dabus Feb 11 '16 at 22:07

1 Answers1

0

I believe your problem is caused by the fact that you are using the error variable twice for different things. It is declared once as the parameter of the block

httpGet(request) { string, error in

and the second time in the catch handler.

} catch let error as NSError {

try to rename one of them, e.g.

} catch let parsingError as NSError {

And there is also another problem, your closure is declared as returning Void but you are trying to return a String from it. On the other hand, your userNameCall should return a String but is not returning anything.

After fixing those problems, I was able to compile your code successfully.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • @tessa Found another problem which is probably caused by a bad understanding of closures and asynchronous calls. – Sulthan Feb 11 '16 at 22:12
  • Sorry- I'm very new to iOS and Swift and programming in general. I was trying to return those strings to my userNameCall- how do I know where to put my returns? Is there any way you could post the code you got to compile correctly? – Tessa Feb 12 '16 at 13:56
  • @tessa The problem is that you cannot return at all from the completion handler. You need to pass a callback block to `userNameCall` that will be executed asynchronously. – Sulthan Feb 12 '16 at 14:26
  • How would I do that? Can you point me to some tutorials that explain more in detail what you're talking about or post the code you got to compile? – Tessa Feb 12 '16 at 14:43