I'm following a tutorial on the new Codable protocol in Swift 4. I'm retrieving JSON from iTunes.
I've setup my Data Model in a file (SearchResult.swift) that conforms to Codable:
class ResultArray: Codable {
var resultCount = 0
var results = [SearchResult]()
}
class SearchResult: Codable {
var artistName = ""
var trackName = ""
var name: String {
return trackName
}
}
In my ViewController (SearchViewController.swift) I'm decoding the JSON. What I don't understand is the reference to Self in ResultArray.self. The result array object is not declared in the View Controller where I'm making the call.
func parse(data: Data) -> [SearchResult] {
do {
let decoder = JSONDecoder()
let result = try decoder.decode(ResultArray.self, from: data)
return result.results
} catch {
print("JSON Error: \(error)")
return []
}
}