UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: I don`t under stand why return is [String: Astronaut] not just [Astronaut]

Forums > 100 Days of SwiftUI

Hi, I would like to know that why return value is [String: Astronaut] not just [Astronaut]? while Mission is decoded just [Mission]

let astronauts: [String: Astronaut] = Bundle.main.decode("astronauts.json")

 let missions: [Mission] = Bundle.main.decode("missions.json")

please explain it to me.

Thanks :)

its "Loading a specific kind of Codable data" in MoonShot project

extension Bundle {
    func decode(_ file: String) -> [String: Astronaut] {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Failed to locate \(file) in bundle.")
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }

        let decoder = JSONDecoder()

        guard let loaded = try? decoder.decode([String: Astronaut].self, from: data) else {
            fatalError("Failed to decode \(file) from bundle.")
        }

        return loaded
    }
}

2      

Because missions.json consists of an array of Mission objects, as indicated by the fact that the outer brackets are square:

[
    {
        "id": 1,
        "crew": [
            {
                "name": "grissom",
                "role": "Command Pilot"
            },
            {
                "name": "white",
                "role": "Senior Pilot"
            },
            {
                "name": "chaffee",
                "role": "Pilot"
            }
        ],
        "description": "..."
    },
    {
        "id": 7,
        "launchDate": "1968-10-11",
        "crew": [
            {
                "name": "schirra",
                "role": "Commander"
            },
            {
                "name": "eisele",
                "role": "Command Module Pilot"
            },
            {
                "name": "cunningham",
                "role": "Lunar Module Pilot"
            }
        ],
        "description": "..."
    },
    etc...
]

While astronauts.json consists of a dictionary of String keys and Astronaut objects, as indicated by the fact that the outer brackets are curly:

{
    "grissom": {
        "id": "grissom",
        "name": "Virgil I. \"Gus\" Grissom",
        "description": "..."
    },
    "white": {
        "id": "white",
        "name": "Edward H. White II",
        "description": "..."
    },
    etc...
}

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.