我如何从AlamoFire返回一些东西?

import Foundation import Alamofire import UIKit import SwiftyJSON class APIClient: UIViewController { let loggedIn = false struct Constants{ static let Domain = "http://gogogo.com" } func authenticate() -> Bool{ println("Authenticating") if let access_token = FBSDKAccessToken.currentAccessToken().tokenString { let parameters = [ "access_token": access_token ] Alamofire.request(.POST, Constants.Domain+"/accounts", parameters: parameters).responseJSON { (req, res, json, error) in if(error != nil) { self.sendReturnToSplash() } else { let json = JSON(json!) println("\(json)") return true //This doesn't work!!!!!! } } }else{ println("No access token to authenticate") } } func sendReturnToSplash(){ let center = NSNotificationCenter.defaultCenter() let notification = NSNotification(name: NotificationCenters.ReturnToSplash, object: self, userInfo: ["":""]) center.postNotification(notification) } } 

正如你所看到的,如果注销成功,我想返回“true”。 但是,XCode给了我一个“虚拟不符合协议布尔文学”

由于您正在使用asynchronous调用,因此应该将您的身份validationfunction视为asynchronous。 我build议使用类似于以下的完成块:

 func authenticate(completion:(success: Bool) -> Void) { println("Authenticating") if let access_token = FBSDKAccessToken.currentAccessToken().tokenString { let parameters = [ "access_token": access_token ] Alamofire.request(.POST, Constants.Domain+"/accounts", parameters: parameters).responseJSON { (req, res, json, error) in if error != nil { self.sendReturnToSplash() completion(success: false) } else { let json = JSON(json!) println("\(json)") completion(success: true) } } } else { println("No access token to authenticate") completion(success: false) } }