UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Lists with Sections

Forums > SwiftUI

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!

1      

Ed follows up with:

I want to make this auto-fill name in from json, right?
As opposed to having to manually type each out.. defeats the point of json, right?

Yes! You want to store a large list of json somewhere convenient.

Then you pull it into your application, decode it, and shove the contents into an array.

But change your thinking a bit! Once you've pulled the JSON in, decoded it, and stuffed it into an array, IT'S NOT JSON data any more! It's an array!

Then when it's all in ONE array, you get to decide how to further parse your data: CONUS, EUCOM, West Coast, Air Bases, etc.

1      

Hacking with Swift is sponsored by Essential Developer

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.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.