![](/img/trans.png)
[英]Why isn't code being executed after/within a second URLSession.shared.dataTask, that is within an initial URLSession.shared.dataTask’s do block? Swift
[英]Swift 3 Compiler Errors with JSON and Dictionaries within URLSession.shared.dataTask
在此特定應用代碼的先前問題在幫助下“已解決”之后,此代碼出現了新問題:
var properties: [String] = []
var unsortedDetails: [String] = []
var contacts: [[String]] = []
var propertySorting: [(key: String, ind: String, link: Bool)] = []
var autolinks: [Bool] = []
並且,在viewDidLoad函數中:
let contactsRequest = URLRequest(url: URL(string: "https://script.google.com/macros/s/AKfycbxOLElujQcy1-ZUer1KgEvK16gkTLUqYftApjNCM_IRTL3HSuDk/exec?id=" + ID + "&sheet=" + Industry.replacingOccurrences(of: " ", with: "+", options: NSString.CompareOptions.literal, range: nil))!)
let contactsTask = URLSession.shared.dataTask(with: contactsRequest as URLRequest)
{(data, response, error) in
do {
let jsonResult: Dictionary = try JSONSerialization.jsonObject(with: data!, options: []) as! [String : Any]
let results = jsonResult[self.Industry] as! [[String : Any]]
for key in results[0].keys {
self.propertySorting.append(key: (key as? String)!, ind: results[0].value(forKey: key as! String) as! String, link: results[1].value(forKey: key as! String) as! String != "X")
}
self.propertySorting.sort(by: {Int($0.ind) < Int($1.ind)})
for property in self.propertySorting {
self.properties.append(property.key)
self.autolinks.append(property.link)
}
for contact in results {
self.contacts.append([])
for key in self.properties {
self.contacts[self.contacts.count-1].append(contact.value(forKey: key) as! String)
}
}
DispatchQueue.main.async(execute: {
let listController = self.viewControllers[0] as! ListController
listController.properties = self.properties
listController.contacts = self.contacts
listController.autolinks = self.autolinks
listController.tableView.reloadData()
})
} catch {
print("FATAL ERROR")
}
}
contactsTask.resume()
現在遇到的問題是: Extra argument 'ind' in call
propertySorting.append(...)時Extra argument 'ind' in call
,並且如果刪除了此(以前有效的) ind
部分,則將為link
返回相同的錯誤
而不是NSArray
和NSDictionary
使用Swift本機Array
和Dictionary
並將keys
與Dictionary
一起使用以獲取所有鍵的數組。
let jsonResult: Dictionary = try JSONSerialization.jsonObject(with: data!, options: []) as! [String : Any]
let results = jsonResult[self.Industry] as! [[String : Any]]
for key in results[0].keys {
}
注意:無需對Swift本機類型對象使用.mutableContainers
選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.