String을 직접 파싱하지 말고
let sampleStringUrl = "<http://www.example.com/landing/index?uid=L011403&token=clZAAWxBL1SP55qpR02FsDesY&success=1&souce=http://fb.com/swift&flag=4>"
if let urlComponent = URLComponents(string: sampleStringUrl) {
let queryItems = urlComponent.queryItems
let successValue = queryItems?.first(where: { $0.name == "success" })?.value
if successValue == nil {
print("Key success not found")
} else {
print("Value of success: \\(successValue!)")
}
print(urlComponent.path)
}
URLComponents로 url 만들 때 유의점
let comp = URLComponents()
comp.scheme = "https"
comp.host = "example.com"
comp.path = "/user" // path에 넣을 때 앞에 / 를 포함해줘야 함
print(comp.url) // <https://example.com/user>