Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire Not Working Properly - Tuple Issue Different number of elements

Using Xcode 7.1

In the Alamofire responseJSON request I cannot put 4 parameters. Below is the code

 let url2 = "https://httpbin.org/get"
    Alamofire.request(.GET, url2).responseJSON{ request, response, JSON, error  in
        print(JSON)
    }

I get this error: Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result)' (aka '(Optional, Optional, Result)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)

If I remove the "error" parameter from responseJSON and run it...the app builds but no json is printed on the console..

  let url2 = "https://httpbin.org/get"
    Alamofire.request(.GET, url2).responseJSON{ request, response, JSON in
        print(JSON)
    }

Console Output

enter image description here

There is no JSON being printed. If you go to sample URL from the code you will see JSON.

I have followed the instructions from GitHub but its not working

like image 274
Anuj Arora Avatar asked Sep 10 '15 16:09

Anuj Arora


1 Answers

Alamofire v1.x had four parameters to the responseJSON closure. Alamofire v2.x had three parameters. Alamofire v3.x now calls the closure with a single parameter, a Response:

Alamofire.request(.GET, url2).responseJSON { response in
    switch (response.result) {
    case .Success(let value):
        print(value)
    case .Failure(let error):
        if let data = response.data, let string = String(data: data, encoding: NSUTF8StringEncoding) {
            print(string)
        }
        print(error)
    }
}

Alternatively, you can use the isSuccess, isFailure, value, data and error computed properties for Result, e.g.:

Alamofire.request(.GET, url2).responseJSON { response in
    print(response.result.value)
}

[This has been updated for the Alamofire 3 syntax. If you need the Alamofire 2 syntax, please refer to this question's revision history.]

like image 144
Rob Avatar answered Sep 19 '22 11:09

Rob