Skip to content

Parsing single parameter in path. #37

Answered by stephencelis
junjielu asked this question in Q&A
Discussion options

You must be logged in to vote

@junjielu This is a surprisingly subtle problem, especially if you want to print the route back into a URL for the purpose of linking. E.g.:

try appRouter.path(for: .book(id: "foo"))
// "/book/foo/???"

What goes in that path component? If this is an important component of the route, it needs to also be in the data model:

enum AppRoute {
  case book(id: String, slug: String)
}

Perhaps this path component is optional, where the segment is omitted when nil:

enum AppRoute {
  case book(id: String, slug: String?)
  // (42, "alice") -> "/book/42/alice"
  // (42, nil)     -> "/book/42"
}

With this data model in place, there's no readymade API for this kind of thing (maybe there should be, but we…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@junjielu
Comment options

Answer selected by junjielu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants