Warm tip: This article is reproduced from serverfault.com, please click

Usage of multiple if-conditions in Menu()

发布于 2020-11-28 09:27:04

I am trying to build a Menu() in SwiftUI which should have at least two if-conditions.

As soon as I implement the second if-conditions the code no longer compiles.

Is this a SwiftUI-Bug, or does someone know what I am doing wrong?

struct ContentView: View {

var departmentList = ["Department 1", "Department 2", "Department 3", "Department 4", "Deprtment 5"]
var salesDepartmentList = ["SalesDepartment 1", "SalesDepartment 2", "SalesDepartment 3"]
var itDepartmentList = ["ITDepartment 1", "ITDepartment 2", "ITDepartment 3"]

var body: some View {
    
    if #available(iOS 14.0, *) {
        VStack {
            Menu("Menu") {
                Menu("Departments") {
                    ForEach(departmentList, id: \.self) { department in
                        if department == "Department 1" {
                            Menu(department) {
                                ForEach(salesDepartmentList, id: \.self) { salesDepartment in
                                    Button(salesDepartment, action: {
                                        print("Do sales stuff")
                                    })
                                }
                            }
                        }
//                            else if department == "Department 2" {
                                Menu(department) {
                                    ForEach(itDepartmentList, id: \.self) { itDepartment in
                                        Button(itDepartment, action: {
                                            print("Do IT stuff")
                                        })
                                    }
                                }
//                            }
                    }
                }
            }
        }
    }
  }
}
Questioner
Kuhlemann
Viewed
0
Asperi 2020-11-28 17:45:15

It just becomes complicated for compiler. The solution is to simplify by separation (always useful).

Here is possible approach. Tested with Xcode 12.1 / iOS 14.1 (you can simplify it even more and far).

struct ContentView: View {
    
    var departmentList = ["Department 1", "Department 2", "Department 3", "Department 4", "Deprtment 5"]
    var salesDepartmentList = ["SalesDepartment 1", "SalesDepartment 2", "SalesDepartment 3"]
    var itDepartmentList = ["ITDepartment 1", "ITDepartment 2", "ITDepartment 3"]
    
    var body: some View {
        
        if #available(iOS 14.0, *) {
            VStack {
                Menu("Menu") {
                    Menu("Departments") {
                        ForEach(departmentList, id: \.self) { department in
                            if department == "Department 1" {
                                salesSubmenu(for: department)
                            }
                            else if department == "Department 2" {
                                itSubmenu(for: department)
                            }
                        }
                    }
                }
            }
        }
    }
    
    func salesSubmenu(for department: String) -> some View {
        Menu(department) {
            ForEach(salesDepartmentList, id: \.self) { salesDepartment in
                Button(salesDepartment, action: {
                    print("Do sales stuff")
                })
            }
        }
    }

    func itSubmenu(for department: String) -> some View {
        Menu(department) {
            ForEach(itDepartmentList, id: \.self) { itDepartment in
                Button(itDepartment, action: {
                    print("Do IT stuff")
                })
            }
        }
    }
}