与预期的不同,URL
即使path
相同,URL
也可能是不同的。因此,应避免使用init(fileURLWithPath: String, isDirectory: Bool, relativeTo: URL?)
,使用appendingPathComponent(_:isDirectory:)
作为替代。
let url = URL(fileURLWithPath: "foo/bar", isDirectory: false)
let baseURL = url.deletingLastPathComponent()
let newURL = URL(fileURLWithPath: "bar", isDirectory: false, relativeTo: baseURL)
let testURL = baseURL.appendingPathComponent("bar")
print(url == newURL) // prints false
print(url.path == newURL.path) // prints true
print(url == testURL) // prints true
print(url.path == testURL.path) // prints true