官方doctor 说这是不受欢迎的:https://developer.apple.com/documentation/foundation/url/1780397-appendpathcomponent

enter image description here

替代方案是什么?

推荐答案

替代的是append(path:directoryHint:)美元.

如果您使用Xcode的代码完成功能,您可以清楚地看到这一点.键入类似以下内容:

someUrl.append

而Xcode则显示了可能的匹配列表.它将显示已弃用的方法,并提到替换方法.

enter image description here

另一种 Select 是在代码中右击appendPathComponent的用法,然后 Select "跳转到定义".这将把您带到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的开发人员文档窗口没有显示替代版本.

Swift相关问答推荐

如何访问用户选定目录下的(只读)文件?

有没有一种方法可以迭代可编码的代码(例如,JSON解析中的每一项)?

在按下按钮动画后执行按钮动作

了解SWIFT中的命名VS位置函数调用

使用MKLocalSearch获取坐标

出错-无法将季节类型的值转换为预期的参数类型';[水果]';

如何将函数参数(字符串文字)标记为可本地化?

物理主体未与 spritekit Swift 的网格图案上的纹理图像对齐

如何在设备(或常量)地址空间中创建缓冲区?

如何隐藏集合视图中特定的单元格,以避免出现空白空间?

如何使用 Swift 在非本地函数中切换()一个 Bool?

为什么我在我的 Swift iOS 应用程序项目中收到 Xcode 中的错误消息无法识别的 Select 器发送到类?

如何打印出此 struct 中的数据?

响应 UITextField (UIViewRepresentable) 中的特定按键

带有屏幕参数的 NSWindow 初始化程序在初始化时导致致命错误

如何在 Swift 中将 XPC 自定义类列入白名单?

UnkeyedEncodingContainer 和 KeyedEncodingContainerProtocol 中的 superEncoder 是什么?

如何在 Xcode 中的 UITests 下以通用方式访问后退栏按钮项?

'NSLog' 不可用:可变参数函数在 swift 中不可用

如何在 Swift 中遍历 struct 属性?