json - 将 JSON 文件添加到 Swift Playgrounds
问题描述
我在 swift 游乐场上创建了一本游乐场书,但我不知道如何将 JSON 文件添加到目录中。您可以将 JSON 文件添加到 swift playground (ipad) 吗?如果是这样,怎么做?
解决方案
单击 swift 右上角的“+”图标,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)
}
推荐阅读
- python - 如何从 html 表单中获取日期到烧瓶中
- excel - 为什么在 VBA 中查找时出现错误?
- excel - 如何从 VBA 中的父数据创建子零件编号
- csv - CSV 的 SAS Proc 导入“第 x 行中 x 的无效数据”
- python-3.x - “如何让用户选择与相同的恶棍重新进行战斗。”
- solid - 如何在 SoLiD 中创建容器?
- ios - 如何将一组 UIButtons 添加到 UIStackview 上?
- php - 如何从php中的数据字符串中提取一系列变量?
- php - Docker 映像内容在 POD 中不可用(Docker/Kubernetes)
- excel - 获取下拉列表选项