gpt4 book ai didi

ios - 我如何从 AlamoFire 返回?

转载 作者:可可西里 更新时间:2023-11-01 00:53:12 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com