2
func postUser(username: String, pass: String) -> Bool {
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        return true
                    case .failure(let error):
                        print(error)
                        return false
                }
            }
        }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Creign
  • 134
  • 10

2 Answers2

2

If you are calling an async method like Alamofire.request, then you need notify when this async method is over with closures

Try with this

func postUser(username: String, pass: String, finishedClosured:@escaping ((Bool)->Void))  {
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        finishedClosured(true)
                    case .failure(let error):
                        print(error)
                        finishedClosured(false)
                }
            }
        }

Use it

 self.postUser(username: "your UserName", pass: "Your Pass") { (result) in
            if(result){
                debugPrint("All is fine")
            }else{
                debugPrint("All is wrong")
            }
        }
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
1

You should not return true or false from this method, since this the asynchronous network call, simply use call backs to get your data outside

func postUser(username: String, pass: String callback: @escaping (Bool) -> Void){
            Alamofire.request("https://someAPI.com/auth/login", method: .post, parameters: ["email": contact, "password": pass], encoding: URLEncoding.default, headers: ["Accept":"application/json"]).responseJSON { (response) in

                switch(response.result) {
                    case .success(let value):
                        let json = JSON(value)
                        print("JSON: \(json)")
                        print(json["data"]["result"])
                        callback(true)  //using this you can send back the data                   

                   case .failure(let error):
                        print(error)
                        callback(false)

                }
            }
           //here you can return true or false but I dont think you should


        }
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15