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给了我一个“Void不符合协议BooleanLiteral”
答案 0 :(得分:4)
由于您使用的是异步调用,因此应将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)
}
}