0
我在我的父類中定義了一個全局數組,我試圖從JSON
文件中調用REST
服務GET
服務GET
填充對象。我的想法是,我定義了全局數組,並填充從JSON文件填充的對象,然後將在我的內部類中使用這些數據。但我注意到數據被填滿了,它正確地得到了數據,數據被輸入到了數組中,但是一切都在方法之外被刪除了。在內部類中,數據不存在,甚至不在網絡請求範圍內。我無法想象問題是什麼。數據從全局數組中消失
這裏是我的數組的初始化的全局數組:
class ContentDeliveryViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate{
public var ListOfFiles: [Asset_Content] = []
public var ListOfTags: [String] = []
這裏是獲取JSON和與這表明括號外的列入報表打印的陣列填充方法該陣列已空。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("short CALLED")
let cell = self.tableView.dequeueReusableCell(withIdentifier: "ContentDeliveryCell", for: indexPath as IndexPath) as! ContentDeliveryCell
var content: AWSContent
//fix this
if(tableView == self.searchDisplayController?.searchResultsTableView){
print("search part called!!")
content = filteredList[indexPath.row]
}else{
content = contents![indexPath.row]
}
if(content.key.contains(IDENTIFIER)){
//
let url = URL(string: URL)
let task = URLSession.shared.dataTask(with: url!){
data, response, error in guard error == nil else{
print(error!)
return
}
guard let data = data else{
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [[String: AnyObject]]
var asset: Asset_Content
for jay in json! {
let category = jay["BIGCATEGORY"] as? String
let diagnosis = jay["DIAGNOSIS"] as? String
let perspective = jay["PERSPECTIVE"] as? String
let name = jay["NAME"] as? String
let title = jay["Title"] as? String
let UnparsedTags = jay["TAGS"] as? String
let filename = jay["FILENAME"] as? String
let tagArray = UnparsedTags?.characters.split(separator: ",")
for tag in tagArray!{
if(self.ListOfTags.contains(String(tag))){
}else{
self.ListOfTags.append(String(tag))
print("TAGCOUNT == ", self.ListOfTags.count)
}
}
asset = Asset_Content(category!, diagnosis!, perspective!, name!, title!, filename!)
self.loadJson(forFile: asset)
print("RightCount === ", self.ListOfFiles.count)// FILLED ARRAY
}
print("FirstCount === ", self.ListOfFiles.count)//FILLED ARRAY
}
task.resume()
cell.isHidden = true
print("FirstCount === ", self.ListOfFiles.count)// EMPTY ARRAY
print("TAGCOUNT == ", self.ListOfTags.count) // EMPTY ARRAY
return cell
}else{
cell.prefix = prefix
cell.content = content
print("KEY" ,content.key)
cell.isHidden = false
return cell
}
}
'cellForRowAt'是錯誤的地方下載和pa rse JSON。你的數組是空的,因爲'dataTask'異步工作。 – vadian
如何在完成數據後加載數據? –