TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: SnowSeeker - Facility name variable

Forums > 100 Days of SwiftUI

@Tony  

I'm not understanding how the name variable in the Facility struct gets set. In the lesson it says "First, we’re going to make Facility itself be Identifiable, which means giving it a unique id property and also storing the facility name somewhere in there." https://www.hackingwithswift.com/books/ios-swiftui/binding-an-alert-to-an-optional-string

The Identifiable protocol only shows var id: Self.ID { get } and nothing about name. I'm not seeing how the name value gets set to be used within the computed properties. Appreciate any info on this.

struct Facility: Identifiable {
    let id = UUID()
    var name: String

    var icon: some View {
        let icons = [
            "Accommodation": "house",
            "Beginners": "1.circle",
            "Cross-country": "map",
            "Eco-friendly": "leaf.arrow.circlepath",
            "Family": "person.3"
        ]

        if let iconName = icons[name] {
            let image = Image(systemName: iconName)
                            .accessibility(label: Text(name))
                            .foregroundColor(.secondary)
            return image
        } else {
            fatalError("Unknown facility type: \(name)")
        }
    }

    var alert: Alert {
        let messages = [
            "Accommodation": "This resort has popular on-site accommodation.",
            "Beginners": "This resort has lots of ski schools.",
            "Cross-country": "This resort has many cross-country ski routes.",
            "Eco-friendly": "This resort has won an award for environmental friendliness.",
            "Family": "This resort is popular with families."
        ]

        if let message = messages[name] {
            return Alert(title: Text(name), message: Text(message))
        } else {
            fatalError("Unknown facility type: \(name)")
        }
    }
}

2      

hi,

when the Resorts are loaded from JSON, each will have a value let facilities: [String], the strings that are some assortment of "Accommodation", "Beginners", and so on for the resort.

in the ResortView, there is a ForEach(resort.facilityTypes) { facility in ... construct that calls for the value of resort.facilityTypes, a computed variable on Resort.

what that computes and returns is facilities.map(Facility.init), and that's where each of the facilities strings is turned into a Facility type.

the use of

var facilityTypes: [Facility] {
    facilities.map(Facility.init)
}

is really just a clever, short-hand way of using the default init() for Facility (which would require setting the name); an expanded version would look like this

var facilityTypes: [Facility] {
  facilities.map({ Facility(name: $0) })
}

hope that helps,

DMG

2      

@Tony  

Ohhh that is clever, thanks so much for taking the time to explain that @delawaremathguy.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.