URL relativeTo

直接看代码

/// Alamofire
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="

/// 将后面的目录截去
var url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: "/", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: "/", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/
} else {
    print("relativeUrl is nil")
}

/// 如果不是/ 就替换最后一个目录 (& * , + = 1 a)等
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: "ok", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/ok
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: "ok", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer/ok
} else {
    print("relativeUrl is nil")
}

/// (# ; ?)直接跟在后面
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: "#", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer#
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: "#", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer/what#
} else {
    print("relativeUrl is nil")
}

/// (.)相当于空字符替换最后一级目录
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: ".", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: ".", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer/
} else {
    print("relativeUrl is nil")
}

/// (:)无任何改变
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: ":", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: ":", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/writer/what
} else {
    print("relativeUrl is nil")
}

/// ([])转码替换
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: "[", relativeTo: url) {
    print(relativeUrl.absoluteString) // http://www.jianshu.com/%5B
} else {
    print("relativeUrl is nil")
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: "]", relativeTo: url) {
    print(relativeUrl.absoluteString) //http://www.jianshu.com/writer/%5D
} else {
    print("relativeUrl is nil")
}

/// 无法生成正确的url -> ("", "\\", " ", "|", "^", "`", "<", ">", "\"", "{", "}")
url = URL(string: "http://www.jianshu.com/writer")
if let relativeUrl = URL(string: "", relativeTo: url) {
    print(relativeUrl.absoluteString)
} else {
    print("relativeUrl is nil") // nil
}
url = URL(string: "http://www.jianshu.com/writer/what")
if let relativeUrl = URL(string: "", relativeTo: url) {
    print(relativeUrl.absoluteString)
} else {
    print("relativeUrl is nil") // nil
}

还不知道原因,如有请指教

你可能感兴趣的:(URL relativeTo)