{"id":22362636,"url":"https://github.com/edurd/navigation-with-enums","last_synced_at":"2026-02-07T22:01:38.648Z","repository":{"id":266325559,"uuid":"898039048","full_name":"edurd/navigation-with-enums","owner":"edurd","description":"An article about navigation in SwiftUI using enums","archived":false,"fork":false,"pushed_at":"2024-12-03T17:25:25.000Z","size":1731,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T21:09:31.729Z","etag":null,"topics":["ios","navigationstack","swift","swiftui"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edurd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-03T17:17:39.000Z","updated_at":"2024-12-04T15:20:22.000Z","dependencies_parsed_at":"2024-12-03T18:28:14.726Z","dependency_job_id":"e87ebeb0-42fd-4fb3-ae3a-d58f43b46c2a","html_url":"https://github.com/edurd/navigation-with-enums","commit_stats":null,"previous_names":["edurd/navigation-with-enums"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/edurd/navigation-with-enums","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edurd%2Fnavigation-with-enums","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edurd%2Fnavigation-with-enums/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edurd%2Fnavigation-with-enums/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edurd%2Fnavigation-with-enums/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edurd","download_url":"https://codeload.github.com/edurd/navigation-with-enums/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edurd%2Fnavigation-with-enums/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29209830,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T21:35:21.898Z","status":"ssl_error","status_checked_at":"2026-02-07T21:35:20.106Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","navigationstack","swift","swiftui"],"created_at":"2024-12-04T17:10:04.202Z","updated_at":"2026-02-07T22:01:38.628Z","avatar_url":"https://github.com/edurd.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# An option for Navigation in SwiftUI\n\nThere are many ways to do Navigation in the SwiftUI framework, here I'm going to show you a way to work with navigation using `Enums`.\n\n![https://images.unsplash.com/photo-1531297484001-80022131f5a1?ixlib=rb-4.0.3\u0026q=85\u0026fm=jpg\u0026crop=entropy\u0026cs=srgb](https://images.unsplash.com/photo-1531297484001-80022131f5a1?ixlib=rb-4.0.3\u0026q=85\u0026fm=jpg\u0026crop=entropy\u0026cs=srgb)\n\n## Configuring Views\n\nFirst, we are going to have some views in our project, each view will only have the title as a `Text` \n\n![Screenshot 2024-12-03 at 10.47.16 AM.png](Screenshot_2024-12-03_at_10.47.16_AM.png)\n\nInside each view we’ll only have this content for now\n\n```swift\nstruct ChildOne: View {\n    var body: some View {\n        Text(\"Child One\")\n    }\n}\n```\n\nNow, the idea is to have an `enum` that conforms the protocol `Hashable` that helps us to move around between views on our project\n\n```swift\nenum NavigationEnum: Hashable {\n    case childOne\n    case childTwo\n    case childThree\n}\n```\n\n\u003caside\u003e\n💡\n\n## Why do you need `Hashable` protocol?\n\nOkay, the short answer would be that NavigationDestination ask you to use it.\n\nThe long answer would be that SwiftUI need `Hashable` in this case to uniquely identify the data type that is being received. Conforming `Hashable` protocol, generates a unique hash value that SwiftUI can use internally to differentiate between destinations. If you're using a `NavigationStack` with a `NavigationPath`, SwiftUI stores and compares these hash values to manage the navigation history efficiently.\n\n\u003c/aside\u003e\n\n## Now, configuring the MainView\n\nWe need to set up the `NavigationStack` and the `.navigationDestination` in our MainView\n\nAlso we need the NavigationLinks to tell the NavigationStack where we need to go.\n\nIn the `NavigationLinks` we can set the enum case we want to go.\n\n```swift\nNavigationLink(value: NavigationEnum.childOne) {\n    Text(\"Go to child one\")\n}\n```\n\nWe need a NavigationPath() in case we need to modify it or append a NavigationDestination to it \n\n```swift\n@State var mainPath = NavigationPath()\nNavigationStack(path: $mainPath) { }\n```\n\nThe completed code should be something like this\n\n```swift\nstruct MainView: View {\n    @State var mainPath = NavigationPath()\n    var body: some View {\n        NavigationStack(path: $mainPath) {\n            VStack {\n                Text(\"Main View\")\n                NavigationLink(value: NavigationEnum.childOne) {\n                    Text(\"Go to child one\")\n                }\n                NavigationLink(value: NavigationEnum.childTwo) {\n                    Text(\"Go to child two\")\n                }\n                NavigationLink(value: NavigationEnum.childThree(object: SomeObject())) {\n                    Text(\"Go to child three\")\n                }\n            }\n            .padding()\n            .navigationDestination(for: NavigationEnum.self) { nav in\n                switch nav {\n                case .childOne:\n                    ChildOne()\n                case .childTwo:\n                    ChildTwo()\n                case .childThree(let object):\n                    ChildThree()\n                }\n            }\n        }\n    }\n}\n```\n\nI’m adding the case in childThree enum case, so the updated code for `NavigationEnum` looks like this.\n\n```swift\nenum NavigationEnum: Hashable {\n    case childOne\n    case childTwo\n    case childThree(object: SomeObject)\n}\n\nstruct SomeObject: Hashable {}\n```\n\nThe `SomeObject` struct should also conforms the `Hashable` protocol in order for it to be used inside the `enum` .\n\n## Our final View\n\nOur View at the end should look like this\n\n![Screenshot 2024-12-03 at 12.07.07 PM.png](Screenshot_2024-12-03_at_12.07.07_PM.png)\n\nWhen pressing in one of the buttons, we can be able to move around our views.\n\n![Screenshot 2024-12-03 at 12.08.43 PM.png](Screenshot_2024-12-03_at_12.08.43_PM.png)\n\nWe can add other buttons inside our Child Two view to navigate to others Views using the same .navigationDestination, it works because it inheriting the parent `NavigationStack`.\n\n```swift\nstruct ChildTwo: View {\n    var body: some View {\n        VStack {\n            Text(\"Child Two\")\n            NavigationLink(value: NavigationEnum.childThree(object: SomeObject())) {\n                Text(\"Go to child three\")\n            }\n        }\n        \n    }\n}\n```\n\n## Final approach\n\nHere if a gif of what we have, you can find the repo here in Github.\n\n![navigation gif.gif](navigation_gif.gif)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedurd%2Fnavigation-with-enums","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedurd%2Fnavigation-with-enums","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedurd%2Fnavigation-with-enums/lists"}