Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Parsing JSON Using Codable Parsing Different JSON Structures Recap: Parsing Different JSON Structures

Raymond Choy
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raymond Choy
iOS Development with Swift Techdegree Graduate 11,187 Points

I need help! I've tried 5 wrong solutions and Google search turns up no answers to this coding challenge

/* I've tried the answers at the bottom comment and none are working. Can someone help me with this one? */

let json = “""
{
"title": "Tomb Raider”,
"genre": “action",
"imdb_url": “https://www.imdb.com/title/tt1365519"
}
""".data(using: .utf8)!
struct Movie: Codable {
let title: String
let genre: String
let imdbUrl: String?
enum CodingKeys: String, CodingKey {
case title
case genre
case imdbUrl = “imdb_url"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) self.title = try container.decode(String.self, forKey: .title) self.genre = try container.decode(String.self, forKey: .genre) self.imdbUrl = try _____________
}
}

/* self.imdbUrl = try container.decode(String.self, forKey: imdb_url)_ self.imdbUrl = try container.decode(String.self, forKey: imdbUrl)_ self.imdbUrl = try container.decode(String.self, forKey: "imdbUrl")_ */

2 Answers

Raymond Choy
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raymond Choy
iOS Development with Swift Techdegree Graduate 11,187 Points

Here is the challenge text: Please fill in the correct answer in each blank provided below.

Fill in the blank(s) below

In the following JSON sample, the key "imdb_url" can occasionally be missing. Complete the init(from:) method to ensure that we account for missing data.

See the video on Missing Data @ 6:10 for decodeIfPresent