0

my json return this

{
"status":"false",
"subjects":
[
{
"id":1,
"subjectName":"English"
},
{
"id":3,
"subjectName":"Mathemetics"
}
]
}

here is the swift code

struct SyllabusSubjects: Codable {
    let subjects: [Subject]
}

struct Subject: Codable {
    let subjectName: String?
}

i am trying to show the list of subjects on a screen when it loads.

func getSubject(pid: String)
    {               
        var url : URL = URL(string: "http://example.com/webapi/Users.php?action=GetParentSubject&pId="+pid)!
        
        let loadsubjectdata = URLSession.shared.dataTask(with: url, completionHandler: dataLoaded) 
        loadsubjectdata.resume()
    }
    
     func dataLoaded(data:Data?, response:URLResponse?, error:Error?){
       
        if let detailData = data{
                    print("detaildata", detailData)
                    let decoder = JSONDecoder()
                    do {
                        let jsondata = try decoder.decode(SyllabusSubjects.self, from: detailData)
                        
                        print(jsondata.subjects)
                        
                    }catch let error{
                        print(error)
                    }
                }else{
                    print(error!)
                }

    }

when i try to print data it shows = 42 bytes along with this error message

keyNotFound(CodingKeys(stringValue: "subjects", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "subjects", intValue: nil) ("subjects").", underlyingError: nil))

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • 1
    Instead of printing `detailData` you could print the json content as `if let s = String(data: detailData, using: .utf8) { print(s) }` and check that you really get the json you expect and not some error response. – Joakim Danielson Mar 23 '22 at 11:59
  • are you using `http` or the required `https` for your url? – workingdog support Ukraine Mar 23 '22 at 12:01
  • @JoakimDanielson it did print the api response and when i print ```print(jsondata.subjects)``` it print this ```[kalude.Subject(subjectName: "English"), kalude.Subject(subjectName: "Mathemetics")]``` but i want to show the subjectName in a list view. i think i am missing something. thank you so much. – Kode Lerner Mar 23 '22 at 12:20
  • on which line do you get the error you showing us? – workingdog support Ukraine Mar 23 '22 at 12:27
  • @workingdogsupportUkraine i am using https its just i missed it while pasting the url. i have figured it out with the help of JoakimDanielson print statement help. thank you so much – Kode Lerner Mar 23 '22 at 12:32
  • I am not sure what this question is about now but if you want to access the decoded data outside the function you need to add a completion handler to it so you can pass it back once the download and decoding is done, see [this question](https://stackoverflow.com/questions/30401439/how-could-i-create-a-function-with-a-completion-handler-in-swift) or [this article](https://www.hackingwithswift.com/quick-start/concurrency/how-to-use-continuations-to-convert-completion-handlers-into-async-functions) – Joakim Danielson Mar 23 '22 at 12:48
  • @JoakimDanielson, i am trying to show the fetched data on a view, here `List() { Text("post.subjectName") } .onAppear { NetworkingService().getSubject(pid: "2") { (posts) in print(posts.subjects.count) // it prints correct } }` its showing onAppear, Can you guide me how i am show that on the list? – Kode Lerner Mar 23 '22 at 14:01
  • @JoakimDanielson, i am able to get the decoded data using completion handler on views .onAppear with our guidence, Thanks again. – Kode Lerner Mar 23 '22 at 14:04

0 Answers0