Ios 如何使用Swift在Alamofire函数中返回true/false?

Ios 如何使用Swift在Alamofire函数中返回true/false?,ios,swift,return,boolean,alamofire,Ios,Swift,Return,Boolean,Alamofire,似乎“return true/false”必须与函数处于同一级别,而不是在另一个函数中(在本例中为Alamofire函数) 在这种情况下,如何根据请求返回的内容在checkifriend函数中返回bool?您的checkifriend()函数与Alamofire请求(异步运行)不在同一线程上运行。您应该使用回调函数/完成处理程序,如下所示: func checkIfFriend()->Bool{ request(.POST, "", parameters:["":""]).res

似乎“return true/false”必须与函数处于同一级别,而不是在另一个函数中(在本例中为Alamofire函数)

在这种情况下,如何根据请求返回的内容在checkifriend函数中返回bool?

您的checkifriend()函数与Alamofire请求(异步运行)不在同一线程上运行。您应该使用回调函数/完成处理程序,如下所示:

func checkIfFriend()->Bool{

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

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

不能从异步任务返回。使用完成处理程序。你可以在我的回答中举个例子:这正是我所需要的。谢谢你,伙计!
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.

        })

}