使用未解决的标识符“JSONEncoder” swift4
问题描述:
例如我只想这个样本数组转换成JSON对象使用未解决的标识符“JSONEncoder” swift4
var test = [String : Any]()
test["title"] = "title"
test["description"] = "description"
test["date"] = Date.init()
,我得到这个错误:
use of unresolved identifier 'JSONEncoder'
print(JSONEncoder (test))
答
您没有使用编码器正确。试试这个
let encoder = JSONEncoder()
let json = try? encoder.encode(test)
引用的应用程序的文件here,唯一的init()方法是这样的,所以你不应该建立在编码器本身,让你的JSON结果。
init()
Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.
您是否进口基金会?你确定你正在运行Swift 4吗?什么是“swift -version”输出? – Hamish