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

Using .\self to filter a navigation view

Forums > SwiftUI

I have what may be a dumb question but I've been pulling my hair out trying to understand this. I am still learning Swift and SwiftUI and some of it hasn't quite clicked yet so I apologize if this seems naive.

I have built navigation into my app based on the Landmark app and other examples. I have a tricky JSON structure that I am trying to understand how to use to navigate.

I want to use the item in .self to filter to the category value that it matches and have the navigation link update accordingly. I just cannot figure out how to do this due to the structure.

This works works great for me to see the items in the array associated with the given day:

Text("Tuesday")
            .font(Font.custom("StencilWWII", size:60))
            ForEach(plan.tuesday, id: \.self) { item in
          Text(item)

The JSON is currently structured like this:

[
    {
        "program" : "6 Month",
        "id": "Block 1",
        "weeks": "1 To 4",
        "description": "In the first block of this program, you’ll build a baseline of general fitness and test yourself to evaluate your strengths and weaknesses.",
        "monday": ["Warmup", "Max Test","Finisher", "Cooldown"],
        "tuesday": ["Warmup", "Challenge", "Finisher", "Cooldown"],
        "wednesday" : ["Warmup", "Max Test" , "Challenge", "Finisher", "Cooldown"],
        "thursday" : ["Warmup", "Challenge", "Finisher", "Cooldown"],
        "friday" : ["Warmup", "Challenge", "Finisher", "Cooldown"],
        "saturday" : ["Rest", "Recovery"],
        "sunday" : ["Rest", "Recovery"]
    }
 ]

With my struct for the JSON setup like this:

import SwiftUI
import CoreLocation

struct Plan : Hashable, Codable, Identifiable {
    var id: String
    var program, weeks, description: String
    var monday, tuesday, wednesday, thursday, friday, saturday, sunday : [String]

}

I am attempting something like this but it's not working and just sends me to the first index in my category data (at least that's happening!) But it's not filtering and I don't have any text outputting . Additionaly I don't think I am setting up the for each correctly:

ForEach(userData.plans.filter {$0.monday.contains(category.id)}) { day in
            NavigationLink(
                destination: WorkoutList(workout: workout,category: category, plan: plan)) {
                //Text(day)
            }

I can get the .\self text to show out of the ForEach but it's not filtering to the category view that I am wanting.

As a note, the monday - sunday arrays include values that are found in the id of my Category data as seen below:

 {
        "id": "Warmup",
        "category" : "warmup",
        "name": "Warmups",

UPDATE When I enter the below code:

ForEach(plan.monday, id: \.self) { item  in
                NavigationLink(destination: CategoryList(category: category, workout: workout, plan: plan))
                Text(item)

I get this error

Missing argument for parameter #1 in call, Insert '<#LocalizedStringKey#>, '

I really appreciate any insight/advice you have on this. I have tried everything I can think of and am jut a bit stuck. Thank you!

3      

Well I would start with testing the JSON decoding. Are you in fact getting all the data and decoding it properly? If the answer to that is yes, then you should look at what the passing of data.

The one most important skill to learn with SwiftUI is passing data. You have your @State, @Binding, @ObservedObject, @StateObject and @EnvironmentObject.

Learning how to choose each is very important. And the wonderful thing is, Paul has added a tool on the site that helps you determine which is best for your case.

Here it is: https://www.hackingwithswift.com/articles/227/which-swiftui-property-wrapper

Now if you need specific help, then you need to share the following: -> Your JSON decoding code -> How you fetch and store the JSON -> CatergoryList -> WorkoutList

The more code you share, the easier it is for people to help out. Typically you should be sharing a repo as it makes it a whole lot easier for us to try things out and provide you with tangible working advice. 😉

4      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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.