URL的appendPathComponent(_:)方法被弃用了吗?

5
1个回答

4
替换为 append(path:directoryHint:)
如果您使用 Xcode 的代码补全功能,就可以清晰地看到这一点。输入类似以下内容的内容:
someUrl.append

并且Xcode将显示可能匹配的列表。它会显示已弃用的方法,并提及替代方案。

enter image description here

另一个选项是在代码中右键单击对appendPathComponent的使用,并选择“跳转到定义”。这将带您进入Foundation.URL的接口文件,您将看到类似以下内容:
/// Appends a path component to the URL.
///
/// - note: This function performs a file system operation to determine if the path component is a directory. If so, it will append a trailing `/`. If you know in advance that the path component is a directory or not, then use `func appendingPathComponent(_:isDirectory:)`.
/// - parameter pathComponent: The path component to add.
@available(macOS, introduced: 10.9, deprecated: 100000.0, message: "Use append(path:directoryHint:) instead")
@available(iOS, introduced: 7.0, deprecated: 100000.0, message: "Use append(path:directoryHint:) instead")
@available(tvOS, introduced: 9.0, deprecated: 100000.0, message: "Use append(path:directoryHint:) instead")
@available(watchOS, introduced: 2.0, deprecated: 100000.0, message: "Use append(path:directoryHint:) instead")
public mutating func appendPathComponent(_ pathComponent: String)

@available 表示替换内容。这就是 Xcode 显示替换的方式。不确定为什么在线文档和 Xcode 的开发者文档窗口没有显示替换。


坏苹果文档。有趣的是,对我来说,Xcode比他们的在线文档提供更多信息。 - Hammerhead
1
@Hammerhead 刚刚更新了答案,加入了更多细节,解释了 Xcode 如何展示它的功能。 - HangarRash
如果你把网站、Xcode和wayback machine结合起来,通常可以得到一个不那么不完整的画面,大约90%的时间都是如此。个人而言,我更喜欢他们在发布时进行干净的下架。 - benc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接