Last active
March 15, 2019 01:20
-
-
Save garsdle/8dedaba2884e405a754f506e182c924e to your computer and use it in GitHub Desktop.
An example of DeepLinking and makeIterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum DeepLink { | |
case post(id: String, commentId: String?) | |
case user(username: String) | |
static func map(url: URL) -> DeepLink? { | |
var deepLink: DeepLink? | |
var componentsIterator = url.pathComponents.makeIterator() | |
_ = componentsIterator.next() //url components usually start with a slash so we just ignore | |
guard let firstComponent = componentsIterator.next() else { return nil } | |
if firstComponent == "posts" { | |
guard let postId = componentsIterator.next() else { return nil } | |
let commentId = componentsIterator.next() | |
deepLink = DeepLink.post(id: postId, commentId: commentId) | |
} else if firstComponent == "users" { | |
guard let username = componentsIterator.next() else { return nil } | |
deepLink = DeepLink.user(username: username) | |
} | |
return deepLink | |
} | |
} | |
let url = URL(string: "https://app.input.com/posts/1337/comments/2286")! | |
print(DeepLink.map(url: url)) | |
let url2 = URL(string: "https://app.input.com/users/manolo")! | |
print(DeepLink.map(url: url2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment