|
I was able to do the first task:
But a bit lost on the second bit:
Any ideas? I'm guessing it's something to do with the way Paul compared the 2 json files to show crewMember.role Regards, Tim. |
|
In the initializer of AstronautView, I added the following:
"missionsFlown" is obviously a new [String] variable in AstronautView, each element being a mission that they participated in. Edit: FYI, I seriously struggled with this for awhile, especially with getting the first(where: ) line to work... If there is a better way to accomplish this, I'd love to know. |
|
Hello Tim,
As you rightly said, it has everything to do with "linking" the astronaut with the Mission array to create a matches array of Mission. Good luck with it. William |
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Hi @ZoydWheeler, Thank you so much, I just got your solution working. I too struggled with the .first(where: ) line, now I can see I was so close! At one stage I could print a single mission but not multiple missions. I hadn't thought of a for in loop with a break. I'm going to try the other solution that William posted now, I didn't even know about .contains(where: ). Also I liked the way you added the mission.json directly into the init, I didn't need to change the preview code. Thanks again, Tim. |
|
Hi William, Thank you for your help. I managed to get a modified version of your code working:
And displayed using:
I'm not sure how to add the crew role like @ZoydWheeler did. I'm guessing something like:
where index is the astronaut ... but really it's beyond the requirements of the challenge. I'll try the third part of the challenge tomorrow. Thanks again, Tim. |
|
Hello Tim, There is always another way to code a solution, that is one of the great things about coding. And there will always be a way to get the data you want out of whatever structure. It helps sometimes to create a playground concentrating on a specific part of the code where it maybe difficult to visualize how to acheive a particular result. Good luck with it. William |
|
Has anyone got an idea for solving the last part of the wrap up to toggle between date and crew. The actual toggle bit seems easy. I can toggle between date and say a VStack of fixed text. For creating the the crew list I was wondering if I overrode the init of content view and ran over all the missions etc. but hitting a dead end. |
|
I just used a first(where:) on the astronauts array in a separate function and then called that function whenever the toggle is set to show crewMembers
The Mission variable 'crewNames' is [String] that I ended up creating for some reason...it's been awhile since I've looked at this code. It just pulls the crew names out of the [CrewRole] into their own array. It's possible you can find a more elegant way to iterate over the crewmember names. Good luck. |
|
Hi, thanks for that. It make sense to me but I get the error against NavigationView of The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions Full code is:
|
|
This was my solution to the second problem, which is similar to what's already been proposed with a few slight modifications that uses code that is more common.
This is my solution to the third problem, and I'm quite proud of it.
I put this code in my Missions.swift file. It is a variable like formattedLaunchDate, but it creates a string that will format all of the crew into one string. I looked up different String operators before finding joined, which fit my needs perfectly! Hopefully you tried and just need inspiration, because that's always the best way to learn. Good luck! |
|
Thanks @nickel-dime for your solver 3 task! It's very simple )
|
|
|
|
@financce doesn't that solution just display the name from mission strruct and not the full name from astronaut? |
|
This is my solution to exercise 3 - took me a while
|
|
I was trying to do this with builtin array functions which are more efficient/faster than building a new array one element at a time. Given how many times the json files are decoded in this project, I hope that Swift makes the redundant decoding more efficient behind the scenes. Perhaps the json files should be decoded in ContentView and made Environment variables? I wouldn't expect multiple decodes of the json file to scale up very well. Here's what I added to AstronautView to get all missions for an astronaut:
Here's what I added to the Mission Struct to get a crew list
And then at the bottom of Mission.swift I added this code which came from one of Paul's examples:
|
|
solution for second problem:
solution for third problem:
|
|
Anyone else finding SwiftUI's layout prioritization way too trigger happy about truncating, shrinking, and/or hiding your views? In the second challenge (adding flown missions to
After solving the data part of the challenge, my first pass at laying out the view was:
Everything appeared, but the text was always truncated and the image was ever slightly shrunk from full size. So I came across a fun little modifier meant to prevent SwiftUI from truncating the text:
With that, both the text and list appeared unharmed by SwiftUI, but bafflingly the image shrunk to 50% its width. So I tried adding But the In the end, I tried every combination of Is it possible in SwiftUI? Can anyone else wrangle SwiftUI into presenting these three views unmolested? |
|
I am finding it works perfectly when you first run it. Then if you add a new item and dismiss the sheet, the label toggles between Edit and Done and the list bounces sideways. If you then swipe an item sideways to reveal the delete button and then swipe it back, the Edit button starts functioning normally again, putting the list into edit mode and showing the red circle - buttons again! |
|
I see your .sheet(isPresented: ) is attached to the list - which happens to be false here because it's initialized to false. Since you want your sheet for AddView to be presented when the button is pressed, I think it belongs in the action of the button. |
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
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.