簡體   English   中英

使用URLSession.shared.dataTask中的JSON和字典進行Swift 3編譯器錯誤

[英]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返回相同的錯誤

而不是NSArrayNSDictionary使用Swift本機ArrayDictionary並將keysDictionary一起使用以獲取所有鍵的數組。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM