Hi I am making an app which works with an API. I have a working code which receives data from the API. But I thought it would be better to make my code a bit cleaner. I want to set the data from the api in an dictionary but I can't get it working. Any help would be appreciated, thanx!
This is my dictionary:
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
}
}
This is what I have now:
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()
})
}
I think that the problem is that it isn't filling my dictionary the write way because when I try to set the dictionary values in a table cell, the dictionary is empty.