0
func checkIfFriend()->Bool{

    request(.POST, "", parameters:["":""]).responseJSON{_,_,jsonData in

       if something{ 
return true}
else{
return false
  }      
}

It appears that "return true/false" has to be in the same level than function is and not inside another function (in this case the Alamofire one).

In that case, how can I return bool in checkIfFriend function depending on what the request return?

A. Sola
  • 107
  • 1
  • 12
  • 1
    You cannot return from an asynchronous task. Use a completion handler. You can take example in my answer here: http://stackoverflow.com/a/35720670/2227743 – Eric Aya Mar 10 '16 at 17:40
  • That was exactly what I needed. Thank you man! – A. Sola Mar 10 '16 at 18:02

1 Answers1

0

Your checkIfFriend() function does not run on the same thread as your Alamofire request (Which runs asynchronously). You should use a callback function/ completion handler as shown below:

func checkIfFriend(completion : (Bool, Any?, Error?) -> Void) {

request(.POST, "", parameters:["":""]).responseJSON{_,_,jsonData in

   if something{ 

    completion(true, contentFromResponse, nil)
    //return true

   }else{

    completion(false, contentFromResponse, nil)
   // return false

   }
}

//Then you can call your checkIfFriend Function like shown below and make use 
// of the "returned" bool values from the completion Handler

override func viewDidLoad() {
   super.viewDidLoad()

    var areWeFriends: Bool = Bool()

    var responseContent: Any = Any()
        checkIfFriend(completion: { (success, content, error) in
            areWeFriends = success // are We Friends will equal true or false depending on your response from alamofire. 
                                   //You can also use the content of the response any errors if you wish.

        })

}
Loup G
  • 169
  • 1
  • 11