let encoder = JSONEncoder()
do {
let encodData = try encoder.encode("test string") // same as Int type
print(encodData) // nil
} catch let err {
print(err.localizedDescription) // The data couldn’t be written because it isn’t in the correct format.
}
Run Code Online (Sandbox Code Playgroud)
如何编码这些类型值
顶级(根)JSON 对象只能是数组或字典。例如:
do {
let encoder = JSONEncoder()
let encodData = try encoder.encode(["test string"])
print(String(data: encodData, encoding: .utf8)!)
// ["test string"]
} catch {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2135 次 |
| 最近记录: |