作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
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 does not conform to protocol BooleanLiteral”
最佳答案
由于您使用的是异步调用,因此您应该将您的身份验证函数视为异步的。我建议使用类似于以下的完成 block :
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/
我是一名优秀的程序员,十分优秀!