Home > AI > IOS > Foundation >

appendPathComponent(_:)

Example 1: assemble the url with slash and it compares the appendPathComponent with URLComponents

var a = URL(string: "http://jobyme88.com")
a?.appendPathComponent("ok", isDirectory: true)

var b = URLComponents(string: "http://jobyme88.com" )
b?.queryItems = [URLQueryItem(name: "api-key", value: "api-values")]
print(a)
print(b)

Example 2: the difference between appendPathComponent and appendingPathComponent.

appendPathComponent would change the original url

appendingPathComponent will not change the original url but will return a new value.

var a = URL(string: "http://jobyme88.com")

let c = a?.appendPathComponent("ok")
print("c: ", c)
print("a: " , a)
print("a?.absoluteURL: ", a?.absoluteURL)
print("a?.baseURL: ", a?.baseURL)
print("a?.relativePath: ", a?.relativePath)
print("a?.relativeString: ", a?.relativeString)


print("\n\n\n")

var b = URL(string: "http://jobyme88.com")
let d = b?.appendingPathComponent("ok")
print("d: ", d)
print("b: ", b)
print("b?.absoluteURL: ", b?.absoluteURL)
print("b?.baseURL: ", b?.baseURL)
print("b?.relativePath: ", b?.relativePath)
print("b?.relativeString: ", b?.relativeString)

 

Leave a Reply