【发布时间】:2018-09-01 11:28:58
【问题描述】:
在使用 Swift 4 解码 JSON 时,我想在解码过程中将字符串转换为大写。 JSON 将其存储为大写
例如
let title = "I CANT STAND THE RAIN"
print(title.capitalized)
如何在解码过程中执行此操作,以便将字符串以大写形式存储在我的模型中?
唯一需要注意的是,我只想将 JSON(标题)中的一个属性大写,而不是其余的。
struct Book: Decodable {
let title: String
let author: String
let genre: String
init(newTitle: String, newAuthor: String, newGenre: String) {
title = newTitle
author = newAuthor
genre = newGenre
}
}
let book = try! decoder.decode(Book.self, from: jsonData)
【问题讨论】:
-
不相关但使用
struct,您无需提供自己的init即可设置所有属性的值。struct默认为您提供。
标签: swift4 jsondecoder