gpt4 book ai didi

swift - Apple Swift 3.0 数据

转载 作者:行者123 更新时间:2023-11-28 09:36:18 25 4
gpt4 key购买 nike

我正在尝试使用:

init(contentsOf: URL, options: Data.ReadingOptions)

在 Swift 3.0 和 xCode 8.0 中。我使用的确切语句是:

let jsonData = try Data.init(contentsOf: URL(file_path), options: .mappedIfSafe)

我也试过:

let jsonData = try Data(contentsOf: URL(file_path), options: .mappedIfSafe)

我正在关注 documentation

这不会编译,提示调用中的参数标签不正确。

我是 Swift 新手,所以请保持温柔。 file_path 是从 NSOpenPanel 获取的字符串,并使用 stringValue 进行字符串化。

谢谢!

最佳答案

不要将字符串路径与 NSOpenPanel 结合使用,获取 url 属性——避免创建 URL 的额外步骤——并包装 Data do - catch block 中的初始值设定项。

if let url = openPanel.url {
do {
let jsonData = try Data(contentsOf: url, options: .mappedIfSafe)
// do things with jsonData
} catch {
print(error)
}
}

关于swift - Apple Swift 3.0 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41642661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com