Enum based alternative to type-erased NavigationPath for NavigationStack

After realizing the lookup and editing limitations of the built-in, type-erased NavigationPath, I’m exploring an enum-based solution. With this approach, I can inspect and edit the path much easier while still combining multiple types.

Besides sharing this solution, I want to know if there are any drawbacks with this approach. Do I lose any functionality that the native NavigationPath offered?

import SwiftUI

struct WithRouteEnumNavigationPath: View {
    @State var navigationPath = [Route]()
    
    var body: some View {
        NavigationStack(path: $navigationPath) {
            Text("Root")
                .navigationDestination(for: Route.self) { route in
                    switch route {
                    case .student(let student):
                        StudentView(student: student)
                    case .teacher(let teacher):
                        TeacherView(teacher: teacher)
                    }
                }
        }
        .overlay {
            VStack {
                Text("Add Student")
                    .onTapGesture {
                        navigationPath.append(.student(Student()))
                    }
                Spacer()
                Text("Add Teacher")
                    .onTapGesture {
                        navigationPath.append(.teacher(Teacher()))
                    }
            }
        }
    }
}

struct StudentView: View {
    let student: Student
    var body: some View {
        Text("Student id (student.id)")
    }
}

struct TeacherView: View {
    let teacher: Teacher
    var body: some View {
        Text("Teacher id (teacher.id)")
    }
}

struct Student: Hashable {
    let id = UUID()
}

struct Teacher: Hashable {
    let id = UUID()
}

enum Route: Hashable {
    case student(_ student: Student)
    case teacher(_ teacher: Teacher)
}

#Preview {
    WithRouteEnumNavigationPath()
}

This second code snippet shows the same scenario with the built-in NavigationPath:

import SwiftUI

struct WithTypeErasedNavigationPath: View {
    @State var navigationPath = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $navigationPath) {
            Text("Root")
                .navigationDestination(for: Teacher.self) { teacher in
                    TeacherView(teacher: teacher)
                }
                .navigationDestination(for: Student.self) { student in
                    StudentView(student: student)
                }
        }
        .overlay {
            VStack {
                Text("Add Student")
                    .onTapGesture {
                        navigationPath.append(Student())
                    }
                Spacer()
                Text("Add Teacher")
                    .onTapGesture {
                        navigationPath.append(Teacher())
                    }
            }
        }
    }
}

struct StudentView: View {
    let student: Student
    var body: some View {
        Text("Student id (student.id)")
    }
}

struct TeacherView: View {
    let teacher: Teacher
    var body: some View {
        Text("Teacher id (teacher.id)")
    }
}

struct Student: Hashable {
    let id = UUID()
}

struct Teacher: Hashable {
    let id = UUID()
}

#Preview {
    WithTypeErasedNavigationPath()
}

0

Do I lose any functionality that the native NavigationPath offered?

Yes. By using a homogeneous navigation path, you effectively made your navigation very centralised.

Suppose when building your app, you found that in addition to teachers and students, you also need to navigate to views representing Schools. If you use a NavigationPath, you can just add a navigationDestination to whichever view that needs to show schools. For example, there might be a SchoolsList view, whose body looks like this:

List(schools) { school in
    NavigationLink(school.name, value: school)
}
.navigationDestination(for: School.self) {
    SchoolView($0)
}

You only need to change the view that is concerned about Schools (SchoolsList in this case), and the other views don’t need to change at all.

If you use a Route enum instead, you need to add an extra case to the enum, and add a extra switch case in the navigationDestination that is applied to the root of the navigation stack. You effectively lose separation of concern – the root of the navigation stack needs to know about every kind of view that will potentially be navigated to.

enum Route: Hashable {
    case student(Student)
    case teacher(Teacher)
    case school(School) // <-----
}

// ...

NavigationStack(path: $navigationPath) {
    Text("Root")
        .navigationDestination(for: Route.self) { route in
            switch route {
            case .student(let student):
                StudentView(student: student)
            case .teacher(let teacher):
                TeacherView(teacher: teacher)
            case .school(let school): // <-----
                SchoolView(school)
            }
        }
}

// SchoolsList will just be a List
List(schools) { school in
    NavigationLink(school.name, value: Route.school(school))
}

Consequently this increases coupling of your views. You cannot easily reuse SchoolsList anymore. Because the list rows in SchoolsList are navigation links of type Route, it must be used in a NavigationStack that handles all Routes, instead of just Schools.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật