GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.