Home > Net >  Can't access a property in a Struct
Can't access a property in a Struct

Time:03-29

I have the struct Subsection, like so:

struct Subsection: Decodable {
    let subsectionName: String
    let subsectionID: Int
}

That is a type in another class MenuInfo, like so:

struct MenuInfo: Decodable {
    let subsections: [Subsection]
    let menuItems: [MenuItem]
}

Which finally is used inside another struct RestaurantInfo, like so:

struct RestaurantInfo: Decodable {
    let restaurantName: String
    let menuInfo: MenuInfo
    
    enum CodingKeys: String, CodingKey {
        case restaurantName = "restaurant_name"
        case menuInfo
    }
}

I created a mock constant:

let restaurantMenuInfo = RestaurantInfo(
    restaurantName: "SUBWAY",
    menuInfo: MenuInfo(
        subsections: [
            Subsection(subsectionName: "SANDWICHES", subsectionID: 1),
            Subsection(subsectionName: "DRINKS", subsectionID: 2),
            Subsection(subsectionName: "DESSERTS", subsectionID: 3)
        ],
        menuItems: [
            MenuItem(menuItemName: "Chicken Sandwich", menuItemDescription: "Bread / Chicken Filé / Cheese", menuItemPrice: 9.99, subsection: 1),
            MenuItem(menuItemName: "BLT", menuItemDescription: "Bread / Bacon / Lettuce / Tomatoes", menuItemPrice: 12.99, subsection: 1),
            MenuItem(menuItemName: "BBQ sandwich", menuItemDescription: "Garlix Bread / BBQ Sauce / Roast Beef", menuItemPrice: 14.99, subsection: 1),
            MenuItem(menuItemName: "Veggie Burger", menuItemDescription: "Bread / Soy Meat / Pickles", menuItemPrice: 17.99, subsection: 1),
            MenuItem(menuItemName: "Mountain Dew", menuItemDescription: "Lemon / Raspberry / Grape", menuItemPrice: 2.99, subsection: 2),
            MenuItem(menuItemName: "Iced Tea", menuItemDescription: "Tea with Lemon", menuItemPrice: 3.99, subsection: 2),
            MenuItem(menuItemName: "Cookie", menuItemDescription: "Chocolate Chip Cookie", menuItemPrice: 1.99, subsection: 3),
            MenuItem(menuItemName: "Cheese Cake", menuItemDescription: "Cheese cake / Blueberry", menuItemPrice: 5.99, subsection: 3),
            MenuItem(menuItemName: "Chips", menuItemDescription: "Potato chips", menuItemPrice: 2.99, subsection: 3)
        ]
    )
)

I am trying to get a stack view to load a .xib final with the subsectionName but I can't access the property subsectionName in the ViewController.

func setUpMenuSubsection() {
    for subsection in restaurantMenuInfo.menuInfo.subsections.subsectionName {
        if let subsectionView = Bundle.main.loadNibNamed("SubsectionView", owner: nil, options: nil)?.first as? SubsectionView {
            subsectionView.setUpSubsection(subsection: subsection)

            stackSubsections.addArrangedSubview(subsectionView)
        }
    }
}

But I get the error:

Value of type '[Subsection]' has no member 'subsectionName' on the second line of my function right on ".subsectionName".

CodePudding user response:

The setup of your types is perfectly fine. However, when we look at the loop you are using:

func setUpMenuSubsection() {
    for subsection in restaurantMenuInfo.menuInfo.subsections.subsectionName { // <-- this line
        if let subsectionView = Bundle.main.loadNibNamed("SubsectionView", owner: nil, options: nil)?.first as? SubsectionView {
            subsectionView.setUpSubsection(subsection: subsection)

            stackSubsections.addArrangedSubview(subsectionView)
        }
    }
}

The error is on the marked line. That is because subsections, which is of type [Subsection], does not have a property subsectionName. If you just want to loop over the subsections (which is probably what you want), you can simply remove .subsectionName, and then to access the name of each subsection, inside the loop you would say subsection.subsectionName like so:

func setUpMenuSubsection() {
    for subsection in restaurantMenuInfo.menuInfo.subsections {
        print(subsection.subsectionName)
    }
}

If, however, you want to loop only over the names, and do not care about the subsections themselves, you can do this:

func setUpMenuSubsection() {
    for subsectionName in restaurantMenuInfo.menuInfo.subsections.map(\.subsectionName) {
        print(subsectionName)
    }
}
  • Related