【问题标题】:URL appendPathComponent(_:) deprecated?URL appendPathComponent(_:) 已弃用?
【发布时间】:2023-02-26 15:41:46
【问题描述】:

官方文档说它已被弃用:https://developer.apple.com/documentation/foundation/url/1780397-appendpathcomponent

什么是替代品?

【问题讨论】:

    标签: swift url


    【解决方案1】:

    替换为append(path:directoryHint:)

    如果你使用 Xcode 的代码完成,你可以清楚地看到这一点。输入如下内容:

    someUrl.append
    

    Xcode 会显示可能匹配项的列表。它将显示已弃用的方法并提及替换方法。

    另一种选择是右键单击 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 刚刚用更多细节更新了答案,解释了 Xcode 如何显示它的作用。
    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 2022-12-23
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多