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 Dynamic Keys and Inheritance Dynamic Coding Keys

Wondering how to solve unwrapping compiler error in swift 4

I'm seeing value of optional type 'Library.LibraryCodingKeys' not unwrapped; did you mean to use '!' or '?'?

I believe my code is the same as in the teacher's example but it's hard to tell exactly because there's no download link

1 Answer

Hi, I have attached my code and hope it will help you.

import Foundation

let json = """
{
  "languages": { 
    "python": {
      "designer": [
        "Guido van Rossum"
      ],
      "released": "20 February 1991"
    },
    "java": {
      "designer": [
        "James Gosling"
      ],
      "released": "May 23, 1995"
    },
    "swift": {
      "designer": [
        "Chris Lattner",
        "Apple Inc"
      ],
      "released": "June 2, 2014"
    },
    "ruby": {
      "designer": [
        "Yukihiro Matsumoto"
      ],
      "released": "1995"
    }
  }
}
""".data(using: .utf8)!

struct Language {
    let name: String
    let designer: [String]
    let releaseDate: String
}

struct Library {
    let languages: [Language]

     struct LibraryCodingKeys: CodingKey {
        let stringValue: String

        init?(stringValue: String) {
            self.stringValue = stringValue
        }

        var intValue: Int? { return nil }
        init?(intValue: Int) { return nil }

        static let languages = LibraryCodingKeys(stringValue: "languages")!
    }

    enum LanguageCodingKeys: String, CodingKey {
        case designer
        case released
    }
}

let decoder = JSONDecoder()

extension Library: Decodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: LibraryCodingKeys.self)

        let languagesContainer = try container.nestedContainer(keyedBy: LibraryCodingKeys.self, forKey: .languages)

        self.languages = try languagesContainer.allKeys.map { key in
            let languageContainer = try languagesContainer.nestedContainer(keyedBy: LanguageCodingKeys.self, forKey: key)

            let languageName = key.stringValue
            let designer = try languageContainer.decode([String].self, forKey: .designer)
            let releaseDate = try languageContainer.decode(String.self, forKey: .released)

            return Language(name: languageName, designer: designer, releaseDate: releaseDate)
        }
    }
} 

let library = try! decoder.decode(Library.self, from: json)
for language in library.languages {
    print(language.name)
}
Xavier D
Xavier D
Courses Plus Student 5,840 Points

Tanx! Proper use of indentations increases readability!

let json = """
{
    "languages":
    {
        "python":
        {
            "designer": [ "Guido van Rossum"],
            "released": "20 February 1991"
        },

        "java":
        {
            "designer": ["James Gosling"],
            "released": "May 23, 1995"
        },

        "swift":
        {
            "designer": [ "Chris Lattner", "Apple Inc"],
            "released": "June 2, 2014"
        },

        "ruby":
        {
            "designer": [ "Yukihiro Matsumoto" ],
            "released": "1995"
        }
    }
}
""".data(using: .utf8)!

XD