2016-03-11 48 views
-2
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} 
} 
+0

你需要澄清你的问题。显示你已经尝试了什么,以及你遇到问题的位置。 –

+0

是的。我想用alamofire在数组中设置参数,但是当试图获取数组时返回nil –

回答

0

确定我有同样的问题,但在客观C.

的问题是你的阵列是回归后继续进行。如果你想看看为什么要使用调试。

解决方案是使用信号量。

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上找到许多关于信号量的信息。 如果它对你有帮助投给我