|
I know this sounds silly, but why does this work? Confusion: when I wrote:
How do I know that Swift will decode and give me the exact [MissionView.CrewMember] type I need? As a counterpoint to this, in AstronautView, I had:
or does Swift automatically give back the data type that I want? It just does this kind of magic? After some thinking, the answer might be this: CrewMember, as a struct, has 2 properties, one is a String called role and the other is an Astronaut called astronaut. That is essentially telling Swift that for an instance of CrewMember, for each role, you have an Astronaut. That is essentially a Dictionary where the role is the String key and the astronaut is the Astronaut value, unique to the role String key. However, what I got when I wrote [MissionView.CrewMember] = Bundle... does not give back a Dictionary type. It gives back an Array of MissionView.CrewMember, which is essentially a Dictionary but not precisely. Code Below: Challenge 2: I replaced the ScrollView in MissionView with an independent SwiftUI View called HoriScroll, and the code is like this: HoriScroll:
MissionView:
|
|
Is it because of the custom initializer of MissionView? Where it takes two parameters, one is a mission of type Mission, the other is an Astronaut which is a Dictionary of [String: Astronaut] type but returns a CrewMember type that has 2 properties, role, and astronaut? That is where equivalency between [String : Astronaut] and CrewMember is established ? |
|
Does this actually work for you:
without generating errors? Also, can you post your MissionView? |
|
Yes, that works for me without generating errors. I let CrewMember to conform to the Codable protocol. MissionView:
|
|
I don't see you actually accessing this property anywhere in your listed code, even though you created it:
It does show up in the Previews section but that gets pulled out when the app is built. To use this you would have to specify something like Self.crew or YourStructName.crew. For instance if you tried the following:
It will crash your code when you press the Test button. At that point, SwiftUI tries to load the file from the bundle and decode it and it's unable to decode it because the json is formatted as a My guess is your ContentView has this:
and those are the properties you're passing among your views. I could create this property:
and it will compile and build and everything will work fine until I run some code that tries to access it, then the whole thing will crash because the type So, the answer to this:
Static properties aren't initialized when an instance of the struct is created, they belong the struct type itself. It works because you never tried accessing the property. https://www.hackingwithswift.com/read/0/18/static-properties-and-methods |
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 February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.