kel*_*lin 2 url path nsurl ios swift
我正在尝试通过3个步骤构建复合网址,但是结果是错误的!
// 1)
let baseURL = URL(string: "http://clapps.clappsa.com")!
print(baseURL.absoluteString)
// http://clapps.clappsa.com
// 2)
let extendedURL = baseURL.appendingPathComponent("public", isDirectory: true)
print(extendedURL.absoluteString)
// http://clapps.clappsa.com/public/
// 3)
let finalURL = URL(string: "/img/3-mentee.jpg", relativeTo: extendedURL)!
print(finalURL.absoluteString)
// http://clapps.clappsa.com/img/3-mentee.jpg
Run Code Online (Sandbox Code Playgroud)
预期结果:
http://clapps.clappsa.com/public/img/3-mentee.jpg
Run Code Online (Sandbox Code Playgroud)
即使我尝试使用absoluteURL的extendedURL是这样的:
let finalURL = URL(string: "/img/3-mentee.jpg", relativeTo: extendedURL.absoluteURL)!
print(finalURL.absoluteString)
// http://clapps.clappsa.com/img/3-mentee.jpg
Run Code Online (Sandbox Code Playgroud)
我会得到相同的结果。
奇怪,但是这种方法可以与其他一些不同的URL一起使用。
您应该先删除/才能获得预期的结果:
let finalURL = URL(string: "img/3-mentee.jpg", relativeTo: extendedURL)!
print(finalURL.absoluteString) // http://clapps.clappsa.com/public/img/3-mentee.jpg
Run Code Online (Sandbox Code Playgroud)
在这里可以找到更多详细的解释。
| 归档时间: |
|
| 查看次数: |
478 次 |
| 最近记录: |