for example I just want to convert this sample array into a JSON object
例如,我只想将此示例数组转换为JSON对象
var test = [String : Any] ()
test["title"] = "title"
test["description"] = "description"
test["date"] = Date.init()
and I get this error:
我收到此错误:
use of unresolved identifier 'JSONEncoder'
print(JSONEncoder (test))
1 个解决方案
#1
1
You are not using the encoder correctly. Try this
您没有正确使用编码器。尝试这个
let encoder = JSONEncoder()
let json = try? encoder.encode(test)
Referencing app's document here, the only init method is like this, so you should not create the encoder itself to get you the JSON result.
在这里引用app的文档,唯一的init方法是这样的,所以你不应该创建编码器本身来获得JSON结果。
init()
在里面()
Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.
使用默认格式设置和编码策略创建新的可重用JSON编码器。
#1
1
You are not using the encoder correctly. Try this
您没有正确使用编码器。尝试这个
let encoder = JSONEncoder()
let json = try? encoder.encode(test)
Referencing app's document here, the only init method is like this, so you should not create the encoder itself to get you the JSON result.
在这里引用app的文档,唯一的init方法是这样的,所以你不应该创建编码器本身来获得JSON结果。
init()
在里面()
Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.
使用默认格式设置和编码策略创建新的可重用JSON编码器。