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

Why this answer wrong?

Given the following JSON and Codable conforming type, fill in the blanks such that decoding is successful without having to define a custom init(from: Decoder)

import UIKit

let json = """
{
  "result": {
    "title": "My First Blog Post",
    "author": "Pasan Premaratne",
    "date": "April 16, 2018"
  }
}
""".data(using: .utf8)!

struct Post: Codable {
    let title: String
    let author: String
    let date: String
}

let decoder = JSONDecoder()

let post = try decoder.decode([[String: Post]].self, from:json)

2 Answers

Paolo Scamardella
Paolo Scamardella
24,828 Points

I ran into the same issue. However, to pass this test and even though it is syntactically wrong, you do not need to add a comma after. Example: decoder.decode([String: Post].self from:json). Notice no comma between parameters? It didn't work for me by adding the comma to separate parameters (which is right).

You'll need to do the same for the rest of the written test.

Scott Ho
Scott Ho
288 Points

You have an extra set of bracket: let post = try decoder.decode([[String: Post]].self, from:json)

Should be: let post = try decoder.decode([String: Post].self, from:json), but this still give you a dictionary and not a type Post.