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!

Here is the api result: enter image description here

I want to set the AutorId and BranchId etc etc in a dictionary. And this is de code which I have now. This is the Project class:

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

}

}

and here I am trying to set it in an dictionary:

func apiRequest() {
    apiRequestHeader()
    var running = false
    let urlProjects = NSURL(string: "https://start.jamespro.nl/v4/api/json/projects/?limit=10")
    let task = session?.dataTask(with: urlProjects! as URL) {
        ( data, response, error) in
        if let taskHeader = response as? HTTPURLResponse {
            print(taskHeader.statusCode)
        }
        if error != nil {
            print("There is an error!!!")
            print(error)
        } else {
            if let content = data {
                do {
                    let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
                    print(dictionary)

                    if let items = dictionary["items"] as? [[String:Any]] {
                        let project = Project(dictionary: items)
                        print(project)
                        self.projects.append(project)

                        DispatchQueue.main.async(execute: {
                            self.tableView.reloadData()
                        })

                    }
                }
                catch {
                    print("Error: Could not get any data")
                }
            }
        }
        running = false
    }


    running = true
    task?.resume()

    while running {
        print("waiting...")
        sleep(1)
    }

}
  • You are unwrapping your items [[String:Any]] and passing that to the init of Product but that expects a [String: Any] . You should do a for loop with your items variable and pass that instead to the init – NlsGladiatus Jul 21 '17 at 09:32
  • @NlsGladiatus Sorry but I don't exactly understand what you mean, can you maybe give me an example of that –  Jul 21 '17 at 09:37
  • `if let items = dictionary["items"] as? [[String:Any]]{ for anItem in items {let project = Project(dictionary:anItem); self.projects.append(project)}}` or something similar. – Larme Jul 21 '17 at 09:39
  • `for item in items { let project = Project(dictionary: item) // Append to list }` – NlsGladiatus Jul 21 '17 at 09:40
  • @Larme Thanx for the reaction! Well I just attempted to do that but it doesn't work. –  Jul 21 '17 at 09:49
  • @R.H try this for _dic in dic["item"] as! NSMutableArray { print(_dic) } – Nidhi Patel Jul 21 '17 at 09:58
  • If you are planning to convert to Swift 4, you should have a look at `Codable` (there's a tag for it) – nathan Jul 24 '17 at 03:50

0 Answers0