|
Hello everyone, following the videos und tutorials from HackingWithSwift I did my very first steps in Swift and SwiftUI. I ended up with a very simple List of Players for my Team and a simple edit-View to make changes. The app itself works fine. Now I started to use a NavigationSplitView to add a Team-Layer in the sidebar. I want to add and delete teams and in addition I want to add and delete players to the teams. Actually I am struggeling over the "ForEach" in the "contet:" part of my ContentView. Question 1: How can I iterate over the teamMembers in my teamsList? Second: in my PlayerAddView I hava a button to add players to the team. Question 2: How can I append players to my teamsList.teamMembers-array? Hope these are not to stupid beginner questions... Here the full code:
|
|
@SwiftiJoe explains his pain:
I like to think of So, ask yourself: What is my It appears you're trying to feed the numbers (0..<3) into your view factory? So how can you create a player out of the integers 0, 1, and 2? This seems like a hopeless task.
Instead, you defined a team to have a collection of player objects.
Instead of feeding integers to your
Keep Coding!Come back and let us know how you solved your logic problem |
|
Thanks for your reply and sorry for the misunderstanding. The ForEach over the numbers was just a dummy. Of course I want to go and see one specific player. Trying your code like this:
I get an errormessage: "Cannot find 'team' in scope". Of course (?) because it is in "teamsList.teams.teamMember". I think there is a simple step missing to solve this...but I don`t get it. |
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. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
In my example, I used In your actual code you defined
Because your Extra Credit Reading Assignment:See -> Renaming ContentView Please read it, then leave a comment! |
|
That made my day. Trying to use the ForEach with my StateObject "teamsList" instead of the "selectedTeam" makes definitely more sense... My first question is solved. My second question was about the "Add"-Button in the PlayerAddView.
I added the follwoing to my PlayerAddView:
In my ContentView I added the Binding to my .sheet in the toolbar:
The Button in the toolbar of my PlayerAddView now looks like:
But now I have a strange behavior. I can add and delete Teams. I can add Players to the Teams and the PlayerAddView recognizes the selectedTeam. But after pushing the Add-Button in my PlayerAddView, the player shows up for a second and then disappears?! Where did it go? For review the complete code:
|
|
@Joe continues his learning adventures!
Ok, am working an answer. Perhaps, in the meantime, you'll reciprocate and write a few sentences here about the four links I suggested? |
|
@Obelix asked to leave a comment on the ViewFactory-Concept In one of your links to the ViewFactory-Concept you recomment to follow 3 questions:
In my opinion – as an absolute beginner – the concept of a ViewFactory may give a helpful picture in my mind on what the ForEach is going to do for me. Following your 3 questions I maybe would have solved my first question on my own. One thing that I always have to think about twice are the TYPEs. What am I giving in and what do I want to get back. Because otherwise I get error messages like
Or something else. @Obelix asked about an opinion on renaming the ContentView I really like your comment
This is definitely not the way we should handle naming of variables, functions or files. For me – following many tutorials – it helps to see/find the entry into an app easily as long as the entry is “ContentView”. But for my own apps I will consider the renaming of the ContentView to a more logical name. Using the “refactor”-function in Xcode it should be an easy step to do. |
|
I already found 1 mistake in my code. My TeamsList-class started like this:
I changed it to:
Now the teams get stored as expected. But the players added to the teams still disappear each time the playerListView is reloaded! Why? |
|
After some more testing, everything seems to be fine. In can add teams and I can add players. Crosschecking with some print-functions to see whats actually in my teams-list and the players-array, the team seems to hold my players in their players-array. But after refreshing the view (e.g. tapping an other team), all players in the first tapped team disapper. My opinion: the Add-Button in the PlayerAddView is wrong? Some suggestions?
|
|
Finally I changed the Code of my Add-Button to:
In addition I changed the removePlayer-function to:
Now everything works as expected. But I am not sure, if this is good coding, or if there is an other and better option. If there is, please let me know. |
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.
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.