0

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.

  • @RAJAMOHAN-S yes it is, but my problem wasn't answered :) –  Jul 21 '17 at 11:05
  • Please look up my answer and describe your exact problem – Rajamohan S Jul 21 '17 at 11:08
  • @RAJAMOHAN-S Well my code is almost the same as yours, and when I print the value "anItem" i get this : ["BranchId": 1001, "Description": Test 2, "Deadline": , "State": 0, "AuthorId": 1213, "ContactId": 1001, "Date": 2017-07-12, "CompanyId": 1001, "Id": 20170009] But when I try to set that in a dictionary it doesn't work –  Jul 21 '17 at 11:13
  • Please don't post duplicate questions. If the original one doesn't have an answer, try to improve it by editing. Thanks. – Eric Aya Jul 21 '17 at 11:13
  • 1
    @Moritz Yeah you are write man, Thanx:) will do that the next time –  Jul 21 '17 at 11:14

0 Answers0