BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

flow of data

Forums > 100 Days of SwiftUI

I don't know if flow of data are the correct words but i try to explain. one of my biggest problem learning to program is keeping with the var the strucs and so on.

At the moment i am in moonshot (proyect 8), and i am havin a hard time keeping with what is what...

The init() in the MissionView i was thinking what is $0.id == member.name ???

  init(mission: Mission, astronauts: [Astronaut]) {
        self.mission = mission

        var matches = [CrewMember]()

        for member in mission.crew {
            if let match = astronauts.first(where: { $0.id == member.name }) {
                matches.append(CrewMember(role: member.role, astronaut: match))
            } else {
                fatalError("Missing \(member)")
            }
        }

        self.astronauts = matches
    }

and while thinking this i start thinkin about how every thing is create, strore and load.

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

This is declare in ContentView, this means that de json is store in let mission? or is it store in the [Mission] array struct??

Once is store then in MissionView we declare

let mission: Mission

this is taking the data from the Mission struc??

I don't know if my aproach is correct.

Hope a ihave explain what my doubt is.

Thank you

3      

I haven't actually looked at this particular code myself, but with regards to the first question:

            if let match = astronauts.first(where: { $0.id == member.name }) {

the $0 refers to the astronaut object in the list of astronauts. It could be:

            if let match = astronauts.first(where: { astronaut in 
                              astronaut.id == member.name }) {

So it looks like the astronaut id field is a name.

3      

Thank you but i do not mean that.

first we create the struct

struct Mission: Codable, Identifiable {

    struct CrewRole: Codable {
        let name: String
        let role: String
    }

    let id: Int
    let launchDate: Date?
    let crew: [CrewRole]
    let description: String

    var displayName: String{
        "Apollo \(id)"
    }

    var image: String {
        "apollo\(id)"
    }

    var formattedLaunchDate: String {
        if let launchDate = launchDate {
            let formatter = DateFormatter()
            formatter.dateStyle = .long
            return formatter.string(from: launchDate)
        } else {
            return "N/A"
        }
    }
}

then we asing to a porperty in the ContentView

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

but if i want to use that data in other view we use (MissionView)

let mission: Mission

what i do not understan is that in the ContentView we press the first mission that pases the data from the struct Mission??to the MissionView?

3      

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

This is declare in ContentView, this means that de json is store in let mission? or is it store in the [Mission] array struct??

The JSON is decoded into an array of Mission structs and stored in the missions variable.

Once is store then in MissionView we declare

let mission: Mission

this is taking the data from the Mission struc??

MissionView displays data from a single Mission, that gets passed into it when it is created with this code:

NavigationLink(destination: MissionView(mission: mission, astronauts: self.astronauts)) {

3      

what i do not understan is that in the ContentView we press the first mission that pases the data from the struct Mission??to the MissionView?

In ContentView, there are two properties, both are arrays, named missions and astronauts. The data in these properties is passed in from the decode method on Bundle.

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

The missions array is passed into List in the NavigationView. List consumes the items in the array one at a time, and each item renders a row in the list. So, a struct that contains all the properties of a single mission is known to each row of the List.

    NavigationView {
        List(missions) { mission in

// NOTE HOW missions inside the parenthesis is the array of Mission, and the mission between the "{" brace and the "in" keyword is a local parameter that contains a single mission.

NavigationLink(destination: MissionView(mission: mission, astronauts: astronauts)) {

When a list row is tapped, the NavigationLink is called with two parameters, a single instance of type Mission, and an array of type Astronauts.

MissionView is initialized with the two parameters copied in in from ContentView, mission, which is a single mission, and astronauts, which is now an array of CrewMember. These values are created in memory by the init inside MissionView.

    let mission: Mission
    let astronauts: [CrewMember]

That is how the data gets from the json files into the MissionView view. Note it gets copied from one scope to another several times, and the astronauts array changes type when passed from ContentView.

Bob

4      

thanx bob that is the part thats taking me so long to understand.

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.