您好我正在制作一个适用于API的应用。我有一个工作代码从API接收数据。但我认为让代码更清洁会更好。我想在字典中设置来自api的数据,但我无法使其工作。任何帮助将不胜感激,谢谢!
这是我的字典:
class Project: NSObject {
var AuthorId: String?
var BranchId: String?
var CompanyId: String?
var ContactId: String?
var Date: String?
var Deadline: String?
var Description: String?
var Id: String?
var State: String?
init(dictionary: [String: Any]) {
self.AuthorId = dictionary["AuthorId"] as? String
self.BranchId = dictionary["BranchId"] as? String
self.CompanyId = dictionary["CompanyId"] as? String
self.ContactId = dictionary["ContactId"] as? String
self.Date = dictionary["Date"] as? String
self.Deadline = dictionary["Deadline"] as? String
self.Description = dictionary["Description"] as? String
self.Id = dictionary["Id"] as? String
self.State = dictionary["State"] as? String
}
}
这就是我现在所拥有的:
let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
//print(dictionary)
if let items = dictionary["items"] as? [[String:Any]] {
for anItem in items {
print(anItem)
let project = Project(dictionary:anItem);
self.projects.append(project)
print(project.AuthorId)
}
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
我认为问题在于它没有填写我的字典写入方式,因为当我尝试在表格单元格中设置字典值时,字典为空。