Json parsing does not recognize struct

I am working on Swift Foundations UI: Mod 5, lesson 3. We are creating a learning app with a .json file containing a variety of data. So far we have created a struct called Module, and a ContentView with a function to parse the .json data. I have written the parsing code exactly as Chris showed in the lesson. Here’s the code:

class ContentModel: ObservableObject {

@Published var modules = [Module]()

init() {
    getLocalData()
}

func getLocalData() {
    //get a url to json file
    let jsonUrl = Bundle.main.url(forResource: "data", withExtension: "json")
    //read the file into a data object
    do {
        let jsonData = try Data(contentsOf: jsonUrl!)
        let jsonDecoder = JSONDecoder()
   
        **let modules = try jsonDecoder.decode([Module].self, from: jsonData)**
          
        //assign parsed modules to modules property
        self.modules = modules
        }
        catch{
            print("JsonDecoder error")
    }
}

}

The problem: I get a parsing error telling me that “Module” is not a struct. This happens in the line that begins and ends in **. Note: the ** is not in the actual code. I have tried cleaning the build folder and restarting Xcode to no avail.

My questions are: Since I have created a struct called Module, why does the parsing function not recognize it? How can I fix this? (Note: Chris does not have this error in his example.)

As always, your helps is valued and appreciated.

And your Module struct conforms to either Decodable or Codable?

Can you post your Module struct code?

Rooster :rooster:, here are the structs we created. They are all Decodable and Identifiable as per the lesson. I have checked for typos etc., but don’t see any. As always another set of eyes is appreciated. Thanks for taking the time to look at this.

Personally, I think Xcode is schizophrenic! :thinking:

struct Module: Decodable, Identifiable {
var id: Int
var category: String
var content: Content
var test: Test
}

struct Content: Decodable, Identifiable {
var id: Int
var image: String
var time: String
var description: String
var lessons: [Lesson]
}

struct Lesson: Decodable, Identifiable {
var id: Int
var title: String
var video: String
var duration: String
var explanation: String
}

struct Test: Decodable, Identifiable {
var id: Int
var image: String
var time: String
var description: String
var question: Question
}

struct Question: Decodable, Identifiable {
var id: Int
var content: String
var correctIndex: Int
var answers: [String]
}

Missing [ ] at
var content: [Content]

Thanks Sparrow :dove:, I’ll fix that and see what happens.

Well I added the brackets and still got the parsing error statement. I checked Chris’ code again and found he did not have any brackets in the variables under Module.

Here’s what Chris had:
// Created by Christopher Ching on 2021-03-04.
//

import Foundation

struct Module: Decodable, Identifiable {

var id: Int
var category: String
var content: Content
var test: Test

}

So I copied and pasted his parsing code into my project and now it works, even though the code was (as far as I can tell) exactly the same as what I had. This is why I think Xcode is schizophrenic or maybe has multiple personalities.

Anyway, your suggestion was totally valid. Thanks for trying to help. Everything works right now.

1 Like