Skip to content

Instantly share code, notes, and snippets.

@mfaani
Last active February 21, 2019 16:38
Show Gist options
  • Save mfaani/6c9dfbc99d96914b4d572a1d1513563b to your computer and use it in GitHub Desktop.
Save mfaani/6c9dfbc99d96914b4d572a1d1513563b to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/7367472/why-does-url-pathcomponents-contain-an/7368129#7368129
import Foundation
let urlString1 = "http://yahoo.com/video/3" // scheme: http | host: yahoo.com | comps ["/", "video", "3"]
let urlString2 = "video/3" // scheme: Empty | host: Empty | comps ["video", "3"]
let urlString3 = "/video/3" // scheme: Empty | host: Empty | comps ["/", "video", "3"]
let urlString4 = "Facebook://Feed/3" // scheme: Facebook | host: Feed | comps ["/", "3"]
let urlString5 = "yahoo.com" // scheme: Empty | host: Empty | comps ["yahoo.com"]
let urlString6 = "giberish://yahoo.com/video/3" // scheme: giberish | host: yahoo.com | comps ["/", "video", "3"]
let urlStrings = [urlString1, urlString2, urlString3, urlString4, urlString5, urlString6]
func logInfo(from urls: [String]) {
urls.forEach { (urlString) in
let url : URL = URL(string: urlString)!
print("scheme: \(url.scheme ?? "Empty") | host: \(url.host ?? "Empty") | comps \(url.pathComponents) ")
}
}
logInfo(from: urlStrings)
@mfaani
Copy link
Author

mfaani commented Feb 21, 2019

The most counter intuitive thing about this is that for /video/3 the pathComponents are 3. ["/", "video", "3"]. See the this SO link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment