在使用 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)