TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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.


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: "\(searchText)&limit=100&entity=song") else { return }
    let (data, _) = try await 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


@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.


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.