Ed has a logic mess!
(1) Basically I'd like the list in sections,
(2) but my if statement's not working either..
(3) The JSON file is just simple, key: string statements..
(4) I'm trying to have the list find from "region" and .......
(5) find the same regions from the json file.
Step away from the code!
Take a break, and step away from your code.
Break your BIG PROBLEM into smaller, solvable problems.
First, it's not JSON data.
Think of your data a little differently. You may have data about bases stored in JSON
format on your iPad, or in the cloud somewhere. Yes, that data is in JSON
format whilst it's in the cloud or stored. But once you've pulled that data from your local file system, or you've downloaded from the interweb then you can STOP thinking about it as JSON
data.
Instead, it's now in an array
within your application. Indeed, I believe you call your array, bases
.
All your base are belong to us.
What's in this large array? I think the answer is clear. It holds both continental US (CONUS) and european (EUCOM) bases. But you want to display them in two lists. Don't try to feed your long list of bases to a List
view. Don't mess with procedural if
statements inside your view code. Instead, declare what you want!
You can keep one array
with all the bases as a reference, but break your big problem down into two smaller problems. Consider computing two specific arrays for your display.
// Break your BIG problem into two smaller problems!
// Simple Base Struct
struct Base {
var baseName: String
var location: String
}
// Turn your JSON into an array of Base objects.
// Hint: It's now an array! It's not JSON anymore.
var bases = [
Base(baseName: "Naval Support Activity: Napoli", location: "EUCOM"),
Base(baseName: "Mayport, Florida", location: "CONUS"),
Base(baseName: "Naval Post Graduate School", location: "CONUS"),
Base(baseName: "Wiesbaden Hospital", location: "EUCOM"),
Base(baseName: "Naval Air Station: Sigonella", location: "EUCOM")
]
// Declare what you want!
// You want an array of EUCOM bases.
var eucomBases: [Base] {
bases.filter{$0.location == "EUCOM"} // <-- find region within your bases array
}
// Declare what you want!
// You want an array of CONUS bases.
var conusBases: [Base] {
bases.filter{$0.location == "CONUS"} // same here.
}
By adding if statements
into your view building code, you're trying to mix procedural programming with SwiftUI's more flexible declarative programming style. You'll need to break this habit.
Once you have two smaller, descriptive arrays, you'll have no problem wrapping them up into sections
in your view.
Keep Coding!