繁体   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