Hello I am fairly new to swiftui and I am creating a NavigationStack from an array of items. Every item in the array is pushed into navigation when I click on the item (I have to click the back button many times to get back to the root) I can't seem to find out why it is doing this.
Main view...
struct ContentView: View {
@State private var BiasStruct: BiasData = BiasData.allBias
var body: some View {
VStack{
NavigationStack{
List{
ForEach(BiasStruct.biases, id: \.self){ entry in
NavigationLink(value: entry){
Text("\(entry.name)")
}
}
.navigationTitle("Biases")
.navigationDestination(for: Biase.self){ bias in
Text("\(bias.name)")
.searchable(text: $searchText)
}
}
}
.padding(8)
}
}
}
Bias struct...
struct BiasData: Codable, Hashable {
var biases: [Biase]
static let allBias: BiasData = Bundle.main.decode(file: "sample2.json")
}
struct Biase: Codable, Hashable {
var id: Int
var name: String
var related, tags, quote, question: [String]
var description, overcome : String
var resourceName, resource: [String]
var example: [String]
var isFaved: Bool? = false
//I created this to set isFaved to false
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(Int.self, forKey: .id)
self.name = try container.decode(String.self, forKey: .name)
self.related = try container.decode([String].self, forKey: .related)
self.tags = try container.decode([String].self, forKey: .tags)
self.quote = try container.decode([String].self, forKey: .quote)
self.description = try container.decode(String.self, forKey: .description)
self.question = try container.decode([String].self, forKey: .question)
self.overcome = try container.decode(String.self, forKey: .overcome)
self.resourceName = try container.decode([String].self, forKey: .resourceName)
self.resource = try container.decode([String].self, forKey: .resource)
self.example = try container.decode([String].self, forKey: .example)
self.isFaved = false
}
}
This is because
Should be defined for
List
, not forForEach
: