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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.