-2

I am learning to decode data with JSON in an Xcode playground but can't figure out what's wrong with my code, I can't return data nor decode it . Here is my code:

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

extension URL {
    func withQueries(_ queries: [String: String]) -> URL? {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
        components?.queryItems = queries.flatMap { URLQueryItem(name: $0.0, value: $0.1) }
        return components?.url
    }
}

struct StoreItems: Codable {
    let results: [StoreItem]
}

struct StoreItem: Codable {
    var name: String
    var artist: String
    var kind: String
    var artworkURL: URL
    var description: String

    enum CodingKeys: String, CodingKey {
        case name = "trackName"
        case artist = "artistName"
        case kind
        case artworkURL
        case description
    }

    enum AdditionalKeys: String, CodingKey {
        case longDescription
    }

    init(from decoder: Decoder) throws {
        let valueContainer = try decoder.container(keyedBy: CodingKeys.self)
        name = try valueContainer.decode(String.self, forKey: CodingKeys.name)
        artist = try valueContainer.decode(String.self, forKey: CodingKeys.artist)
        kind = try valueContainer.decode(String.self, forKey: CodingKeys.kind)
        artworkURL = try valueContainer.decode(URL.self, forKey: CodingKeys.artworkURL)

        if let description = try? valueContainer.decode(String.self, forKey: CodingKeys.description) {
            self.description = description
        } else {
            let additionalValues = try decoder.container(keyedBy: AdditionalKeys.self)
            description = (try? additionalValues.decode(String.self, forKey: AdditionalKeys.longDescription)) ?? ""
        }
    }
}

func fetchItems(matching query: [String: String], completion: @escaping ([StoreItem]?) -> Void) {

    let baseURL = URL(string: "https://www.itunes.apple.com/search?")!

    guard let url = baseURL.withQueries(query) else {
        completion(nil)
        print("Unable to build URL with supplied queries.")
        return
    }

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        let decoder = JSONDecoder()
        if let data = data,
            let storeItems = try? decoder.decode(StoreItems.self, from: data) {
            completion(storeItems.results)
        } else {
            print("Either no data was returned or data was not properly decoded.")
            completion(nil)
            return
        }
    }
    task.resume()
}

let query: [String: String] = [
    "term": "Inside Out 2015",
    "media": "movie",
    "lang": "en_us",
    "limit": "10"
]

fetchItems(matching: query) { (items) in
    print(items)
}

And here is what's printed to the console which I guess shows that something is wrong with my "task":

Either no data was returned or data was not properly decoded.

nil

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Wizzardzz
  • 781
  • 1
  • 9
  • 32
  • 6
    Please, please **do not ignore the error**. `JSONDecoder` throws several different errors describing the problem(s) pretty well. – vadian Dec 16 '17 at 16:29
  • Thank you for your answer Vadian but there is no error, just no result... – Wizzardzz Dec 16 '17 at 16:45
  • 3
    Definitely there **is** an error, but you are ignoring it with the `try?` syntax. Implement a `do - catch` block and read the error. – vadian Dec 16 '17 at 16:47
  • 3
    There is no error because you make the compiler turn the error into a `nil` value using `try?`. Change `let storeItems = try? decoder.decode(StoreItems.self, from: data)` to `do {let storeItems = try decoder.decode(StoreItems.self, from: data)} catch {print(error)}` and include the error in the question. You should also include the actual JSON response in the question when asking for JSON parsing help. – Dávid Pásztor Dec 16 '17 at 16:47
  • When I implement the do - catch I get the following warning on the line: Initialization of immutable value 'storeItems' was never used; consider replacing with assignment. No error, replacing with _ fix the warning but then nothing happens, the playground seems to be running indefinitely – Wizzardzz Dec 16 '17 at 17:33

2 Answers2

2

This is no concrete solution but a suggestion how to debug the decoding.

Replace the entire expression starting with if let data = data, with the code below and debug the code. The potential (in your case definite) error returned from the dataTask is handled, too.


guard let data = data else { 
    print(error!)
    completion(nil)
    return 
}
do {
    let storeItems = try decoder.decode(StoreItems.self, from: data)
    completion(storeItems.results)
} catch DecodingError.dataCorrupted(let context) {
    print(context.debugDescription)
} catch DecodingError.keyNotFound(let key, let context) {
    print("Key '\(key)' not Found")
    print("Debug Description:", context.debugDescription)
} catch DecodingError.valueNotFound(let value, let context) {
    print("Value '\(value)' not Found")
    print("Debug Description:", context.debugDescription)
} catch DecodingError.typeMismatch(let type, let context)  {
    print("Type '\(type)' mismatch")
    print("Debug Description:", context.debugDescription)
} catch {
    print("error: ", error)
}
completion(nil)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    Thank you very much Vadian, your debug code is worth a lot to me. I am getting a nil from the guard completion, then nothing else. Is the problem from the data? – Wizzardzz Dec 16 '17 at 18:38
  • I updated the answer to print the error in the `else` clause of the `guard` statement. As Rob mentioned in his answer the URL is wrong. And he's providing a complete solution. – vadian Dec 16 '17 at 18:40
1

A couple of issues:

  1. The URL is wrong. There is no www.
  2. The artworkURL appears to be optional, as your search didn't return a value for that key.

When I fixed those, it works:

extension URL {
    func withQueries(_ queries: [String: String]) -> URL? {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
        components?.queryItems = queries.flatMap { URLQueryItem(name: $0.0, value: $0.1) }
        return components?.url
    }
}

struct StoreItems: Codable {
    let results: [StoreItem]
}

struct StoreItem: Codable {
    var name: String
    var artist: String
    var kind: String
    var artworkURL: URL?
    var shortDescription: String?
    var longDescription: String?

    enum CodingKeys: String, CodingKey {
        case name = "trackName"
        case artist = "artistName"
        case kind, artworkURL, shortDescription, longDescription
    }
}

enum FetchError: Error {
    case urlError
    case unknownNetworkError
}

func fetchItems(matching query: [String: String], completion: @escaping ([StoreItem]?, Error?) -> Void) {

    let baseURL = URL(string: "https://itunes.apple.com/search")!

    guard let url = baseURL.withQueries(query) else {
        completion(nil, FetchError.urlError)
        return
    }

    let task = URLSession.shared.dataTask(with: url) { data, _, error in
        guard let data = data, error == nil else {
            completion(nil, error ?? FetchError.unknownNetworkError)
            return
        }

        do {
            let storeItems = try JSONDecoder().decode(StoreItems.self, from: data)
            completion(storeItems.results, nil)
        } catch let parseError {
            completion(nil, parseError)
        }
    }
    task.resume()
}

And:

let query = [
    "term": "Inside Out 2015",
    "media": "movie",
    "lang": "en_us",
    "limit": "10"
]

fetchItems(matching: query) { items, error in
    guard let items = items, error == nil else {
        print(error ?? "Unknown error")
        return
    }

    print(items)
}

Note, I'd suggest you add the error to the completion handler so that you can see why it failed (in your case, the first issue was that the URL was wrong).

Rob
  • 415,655
  • 72
  • 787
  • 1,044