-2

I am trying to build an iOS app in Xcode 9.2, Swift 4 that uses a Collection View Cell to display each of the categories in the JSON file. I was working through this tutorial as an example - https://www.youtube.com/watch?v=hPJaQ2Fh7Ao, but I am getting an error:

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))

I have tried several different things, but I am stuck (and new to this). Any suggestions as to what I am missing or doing wrong would be great. Thanks!

JSON API:

{
  "category": [
    {
      "id": 216,
      "title": "Spring"
    },
    {
      "id": 219,
      "title": "Earth Day"
    },
    {
      "id": 114,
      "title": "Admin. Professionals' Day"
    }
  ]
}

ViewController.swift:

import UIKit

struct Category: Codable {
    let category: [CategoryItem]
}

struct CategoryItem: Codable {
    let id: Int
    let title: String

    enum CodingKeys: String, CodingKey {
        case id, title
    }
}

class ViewController: UIViewController, UICollectionViewDataSource {

    var categories = [Category]()

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self

        let jsonUrlString = "https://apis.*************/***/****/********"
        guard let url = URL(string: jsonUrlString) else { return }

        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }

            if err == nil {
                do {
                    let decoder = JSONDecoder()
                    let ecardcategory = try decoder.decode([Category].self, from: data)
                    self.categories = ecardcategory
                } catch let err {
                    print("Err", err)
                }

                DispatchQueue.main.async {
                    //print(self.categories.count)
                    self.collectionView.reloadData()
                }
            }
        }.resume()

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return categories.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as? CustomCollectionViewCell
        //cell?.nameLbl.text = categories[indexPath.row].title
        return cell!
    }
}
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
cjochum
  • 79
  • 2
  • 7
  • 1
    Try changing `[Category].self` to `Category.self`. This of course will require how you declare `categories`. – rmaddy Apr 10 '18 at 16:13
  • I am not sure what you mean by "how you declare categories". I have it set as a variable right below the class line ... var categories = [Category]() . – cjochum Apr 10 '18 at 16:19
  • How is this specific to a collection view cell?... – EmilioPelaez Apr 10 '18 at 16:23
  • @cbartell Your decoded object will only be a single `Category` object, not an array. – rmaddy Apr 10 '18 at 16:27

1 Answers1

1

The error

"Expected to decode Array but found a dictionary instead."

is very clear: The root object is a dictionary (represented by {}), so remove the brackets.

let ecardcategory = try decoder.decode(Category.self, from: data)

You have to declare the data source array

var categories = [CategoryItem]()

and populate it

self.categories = ecardcategory.category
vadian
  • 274,689
  • 30
  • 353
  • 361
  • When I do that, I get "Cannot assign value of type 'Category' to type '[Category]'" on the line "self.categories = ecardcategory". – cjochum Apr 10 '18 at 16:18
  • Thanks so much! I've spent a few days trying to sort through this. :) – cjochum Apr 10 '18 at 16:31