2017-08-30 45 views
-1

我學到迅速不久前,我有這樣的代碼打印數組中的一些問題:「在主線程打印陣列swift3的Xcode 8」

class InitController: UIViewController { 
public var initdata = [(id: String, name: String)]() 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    print("initdata", initdata) 
    init_auth() 

} 
func init_auth(){ 
    let json: [String: Any] = ["token": UserDefaults.standard.value(forKey: "token"), "id":UserDefaults.standard.value(forKey: "id")] 
    //let jsonData = try? JSONSerialization.data(withJSONObject: json) 
    let jsonData = try? JSONSerialization.data(withJSONObject: json) 
    let url = "http://some_ip/api" 
    var request = URLRequest(url: URL(string: url)!) 
    request.httpMethod = "POST" 

    request.httpBody = jsonData 
    request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.addValue("application/json", forHTTPHeaderField: "Accept") 
    //print(jsonData) 

    let task = URLSession.shared.dataTask(with: request) { data, responce, error in guard let data = data, error == nil else { 
     print(error?.localizedDescription ?? "No data") 
     return 
     } 
     let responceJSON = try? JSONSerialization.jsonObject(with: data, options: []) 
     if let responceJSON = responceJSON as? [String: Any]{ 
      let s = responceJSON["specialities"] as? NSDictionary 
      let d = s?["items"] as! NSArray 
      for i in d { 
       let f = i as? NSDictionary 
       self.initdata.append((id: f?["id"] as! String, name: f?["name"] as! String)) 
      } 
     } 
    } 
    print("list", self.initdata) 
    task.resume() 
} 

} initdata是空數組,但我得到了一些JSON文件並追加這個數組。我嘗試打印initdata,但這個數組是零。我想可能是線程中的問題,但我不知道如何實現這一點。

+1

你應該看看[問]和[MCVE] – pvg

+0

URLSession是異步的。在下載之前很長時間很長時間纔打印initData。並請添加一些錯誤處理。每一個!是你的應用程序可能崩潰的一個點。至少學習「如果讓」。 – gnasher729

回答

0

試試這個 -

func init_auth(){ 
    let json: [String: Any] = ["token": UserDefaults.standard.value(forKey: "token"), "id":UserDefaults.standard.value(forKey: "id")] 
    //let jsonData = try? JSONSerialization.data(withJSONObject: json) 
    let jsonData = try? JSONSerialization.data(withJSONObject: json) 
    let url = "http://some_ip/api" 
    var request = URLRequest(url: URL(string: url)!) 
    request.httpMethod = "POST" 

    request.httpBody = jsonData 
    request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.addValue("application/json", forHTTPHeaderField: "Accept") 
    //print(jsonData) 

    let task = URLSession.shared.dataTask(with: request) { data, responce, error in guard let data = data, error == nil else { 
     print(error?.localizedDescription ?? "No data") 
     return 
     } 
     let responceJSON = try? JSONSerialization.jsonObject(with: data, options: []) 
     if let responceJSON = responceJSON as? [String: Any]{ 
      let s = responceJSON["specialities"] as? NSDictionary 
      let d = s?["items"] as! NSArray 
      for i in d { 
       let f = i as? NSDictionary 
       self.initdata.append((id: f?["id"] as! String, name: f?["name"] as! String)) 
      } 
      print("list", self.initdata) 
     } 
    } 
    task.resume() 
}