My Swift 5 (Xcode 11.5) app saves people in a text file (json format).
The struct
s I use to decode the json text:
struct People:Codable {
var groupName:String
var groupLocation:String
var members:[Person]
init(_ gn:String,_ gl:String,_ m:[Person] {
groupName = gn
groupLocation = gl
members = m
}
struct Person:Codable {
var name:String
var age:Int
var profession:String?
init(_ n:String,_ a:Int,_ p:String?) {
name = n
age = a
profession = (p==nil) ? "none" : p!
}
}
Decoding the contents of the text file:
let jsonData = Data(text.utf8)
let decoder = JSONDecoder()
let people = try decoder.decode(People.self, from: jsonData)
let members = people.members
for (i,member) in members.enumerated() {
//print(member.profession==nil)
//Do stuff
}
profession
was added later on and there also might be more values added in the future but I want my app to be backwards compatible to older files. If profession
doesn't exist, it should use "none"
instead (see code above) but when I check member.profession
after decoding, it's still nil
. name
, age
,... all contain the right values, so that part works.
How do I give profession
a value in the struct if it doesn't exist in the json file? What's the simplest/cleanest way to do this, so I can also add to it later on, if necessary (e.g. birthday, gender,...)?