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给我一个“无效不符合协议BooleanLiteral”的信息
最佳答案
由于您使用的是异步调用,因此应将您的authenticate函数视为异步的。我建议使用类似于以下内容的完成块:
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)
}
}
关于ios - 我如何从AlamoFire退货?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29452563/