UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hacking with macOS book - Project 7 (Fast Track) JSON decoder error

Forums > macOS

Environment: macOS 14.0 Sonoma, Swift 5.9, Xcode 15.0.1.

I'm working thru the subject project and I get an error in this statement

let searchResult = try JSONDecoder().decode(SearchResult.self, from: data)

If I run Paul's downloaded solution (just in case I typed something wrong)I get the same error. Paul provides a generic error view, specifucally:

"Sorry, your search failed – please check your internet connection then try again."

I added a comprehensive do | catch to the JSONDecoder statement to get the actual error:

Key 'CodingKeys(stringValue: "previewUrl", intValue: nil)' not found: No value associated with key CodingKeys(stringValue: "previewUrl", intValue: nil) ("previewUrl"). codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 2", intValue: 2)]

Any help would be greatly appreciated.

3      

Hi @vjcinajr

The error you are getting indicates that the JSON data does not have any value for previewUrl. Perhaps the data is malformed or incomplete. Check that your Track model matches the JSON data coming back from the server.

I got project 7 up and running.

I thought Id share my whole performSearch() method in case its of use to you.

func performSearch() async throws {
    guard let searchText = searchText.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
    guard let url = URL(string: "https://itunes.apple.com/search?term=\(searchText)&limit=100&entity=song") else { return }
    let (data, _) = try await URLSession.shared.data(from: url)
    let searchResult = try JSONDecoder().decode(SearchResult.self, from: data)
    tracks = searchResult.results
}

I could probably put the entire finished project up on GitHub if it would help? although if Paul's didn't work.. that is strange indeed.

Mine runs without issue for me since installing XCode 15.01 and Mac OS Sonoma 14.0

3      

@KitchenCrazy, thanks for the response. I changed nothing in thecode but it is working now! The only thing I can think of is that the system was re-booted. I guess something in memory may have been corrupted and was fixed by a system restart. Anyway, thanks for responding.

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.