给定一个这样的文件名:
let fileName = "example.jpg"
我正在使用辅助函数获取此目录的 URL,该函数将 URL 返回给我:
let imagesURL = getURL()
如何将字符串
fileName
附加到 imagesURL
以便它返回如下内容:
/path/to/images/dir/example.jpg
如何获取字符串和 URL 格式?
这将为您提供一个附加了文件名的新 URL:
let appended = imagesURL.appendingPathComponent(fileName)
这会将其转换回字符串:
let strVersion = appended.absoluteString //full URL
let strVersion2 = appended.path //path only
url.appendingPathComponent(fileName)
已弃用,请使用
url.appending(path: filename)
代替。
appendingPathComponent(fileName)
已被弃用,如果您需要与早期版本一起使用,请使用#available属性
if #available(iOS 16, *) {
url.appending(path: fileName)
}
else {
url.appendingPathComponent(fileName)
}