swift alamofire return 值为空

swift alamofire return value is empty

您好,我有以下方法:

    func getAlamoPlayers() ->[Player]{


    //Get TeamID
    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var TeamId:String = prefs.stringForKey("TEAMID")!

    //create parameters for POST
    let parameters =
    ["IdTeam": TeamId]

    var result: NSArray?

    //Request
    Alamofire.request(.POST, "XXXXXX.php", parameters: parameters)

        .responseJSON { (request, response, json, error) -> Void in

            result = (json as NSArray)
            // Make models from Json data
            for (var i = 0; i < result?.count; i++) {
                let dic: NSDictionary = result![i] as NSDictionary
                //Create a team object
                var p:Player = Player()

                //Assign the values of each key value pair to the team object
                p.PlayerID    = dic["IdPlayer"] as String
                p.PlayerName  = dic["name"] as String
                p.PlayerFkToTeam = dic["teams_IdTeam"] as String

                //Add the team to the player array
                self.players.append(p)

            }

    }

    return self.players
}

所以当我在另一个 class 中调用此方法时,玩家数组是空的!如何处理alamofire的异步请求?我认为方法 returns 播放器对象在请求完成之前,还是我错了?

提前致谢!

这里的问题基本上和你问的一样。

AlamoFire GET api request not working as expected

所以基本上发生的事情是您的 post 请求在后台队列中被调用。所以把它想象成调用你的 post 然后继续到下一行。因此,由于它在后台线程中,它不会在您的函数 returns.

之后设置变量

您可以使用我提供的 link 中详细解释的完成处理程序。另一种解决方案是使用 NSNotificationCenter。设置观察者,然后在 POST 请求中 post 通知。

例如: 首先在 class 的顶部声明您的通知名称,您将在其中拥有 post 响应功能。以便其他 classes 可以访问它

let YOUR_NOTIFICATION_NAME = "NOTIFICATION NAME" class YOUR_CLASS{

然后在 class 中写入您要在 POST 完成后执行的代码。

func YOUR_FUNCTION{ ... }

那么在你执行你的 POST 请求之前你想要添加一个观察者。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "YOUR_FUNCTION", name: YOUR_NOTIFICATION_NAME, object: nil)

然后 post 你的 POST 请求中的通知。

Alamofire.request(.POST, "XXXXXX.php", parameters: parameters)
    .responseJSON { (request, response, json, error) -> Void in
         ...<your code>...
   self.players.append(p)
   NSNotificationCenter.defaultCenter().postNotificationName(YOUR_NOTIFICATION_NAME, object: self)
}

所以我按以下方式构建它:

Modell Class 所有请求:

let finishedPlayers = "finishedPlayers"
class StrafenkatalogModel: NSObject {
.
.
func getAlamoPlayers()-> [Player] {

    //Get TeamID
    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var TeamId:String = prefs.stringForKey("TEAMID")!

    //create parameters for POST
    let parameters =
    ["IdTeam": TeamId]

    var result: NSArray?

    //Observer
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "returnPlayers", name: finishedPlayers, object: nil)
    //Request
    Alamofire.request(.POST, "http://smodeonline.com/XXXXXXX.php", parameters: parameters)

        .responseJSON { (request, response, json, error) -> Void in



            result = (json as NSArray)
            // Make models from Json data
            for (var i = 0; i < result?.count; i++) {
                let dic: NSDictionary = result![i] as NSDictionary
                //Create a team object
                var p:Player = Player()

                //Assign the values of each key value pair to the team object
                p.PlayerID    = dic["IdPlayer"] as String
                p.PlayerName  = dic["name"] as String
                p.PlayerFkToTeam = dic["teams_IdTeam"] as String

                //Add the team to the teams array
                self.players.append(p)


            }

    }
    return players  
}


func returnPlayers() ->[Player]{
    return self.players
}

然后我想在TableViewController中调用它class (Class2):

class AddPlayersTableViewController: UITableViewController {

let model:StrafenkatalogModel = StrafenkatalogModel()
var players:[Player] = [Player]()


override func viewDidLoad() {
    super.viewDidLoad()


    // Get Player objects from the model
     NSNotificationCenter.defaultCenter().postNotificationName("finishedPlayers", object: self)
    self.players = self.model.returnPlayers()
    .
    .
    }

但是玩家对象是空的。你知道为什么吗?我有什么不对吗?