【问题标题】:How set variable in Alamofire and get to another function如何在 Alamofire 中设置变量并获取另一个函数
【发布时间】:2016-03-11 15:32:27
【问题描述】:
import Alamofire
import SwiftyJson

public class MyExample{

var myarray=[String]()

func onCreate(){
Alamofire.request(.GET, "https://myurl.org/get", ).responseJSON { response in
            let json = JSON(value)
            let list: Array<JSON> = json.arrayValue
for element in list{
  myarray.append(element["name"].stringValue)
         }

}
func getMyArray(){return myarray}
}

【问题讨论】:

  • 您需要澄清您的问题。展示您尝试过的方法以及遇到问题的地方。
  • 是的。我想用 alamofire 在数组中设置参数,但是当尝试获取数组时返回 nil

标签: ios swift alamofire


【解决方案1】:

好的,我遇到了同样的问题,但在目标 C 中。

问题是您的数组在返回后被填充。如果你用 ant 来看看为什么要使用 debug。

解决方案是使用信号量。

import Alamofire
import SwiftyJson

public class MyExample{

var myarray=[String]()
//create a semaphore and init it
func onCreate(){
Alamofire.request(.GET, "https://myurl.org/get", ).responseJSON { response in
            let json = JSON(value)
            let list: Array<JSON> = json.arrayValue
for element in list{
  myarray.append(element["name"].stringValue)
// use the semaphore
 }

    }
//use function to wait until the semaphore is done by the other thread.
    func getMyArray(){return myarray}
    }

你可以在 google 上找到很多关于信号量的信息。 如果它帮助你投票给我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    相关资源
    最近更新 更多