将 JSON 文件添加到 Swift Playgrounds



我已经在 swift playgrounds 上创建了一本游乐场书,但我不知道如何将 JSON 文件添加到目录中。您可以将 JSON文件添加到 swift playgrounds (ipad( 吗?如果是这样,如何?

单击快速Playgrounds右上角的"+"图标,然后单击Insert from...选项选择 JSON 文件。然后使用以下代码从json文件中读取数据。

import Foundation
do {
guard let fileUrl = Bundle.main.url(forResource: "filename", withExtension: "json") else { fatalError() }
let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
print(text)
} catch {
print(error)
}

最新更新