Say you have a simple
struct Stuff // :Codable ???
{
a: String
b: String
c: String
}
var stuff: Stuff
You have messages that look like this
msg = "b Peach"
in the example stuff.b should be set to "Peach".
(That sentence is explicit. So, stuff.a and stuff.c would not be changed.)
Naive code would have a long switch statement ...
func processMessage
vv = msg.split
switch vv[0] {
case "a": stuff.a = vv[1]
case "b": stuff.b = vv[1]
case "c": stuff.c = vv[1]
default: print incorrect key!
}
Note that that code would have stuff.b set to "Peach", stuff.a and stuff.c would not be changed.
Of course, this is very clumsy.
Surely there's a better way to do this in Swift - perhaps with Decodable
?
--
PS Obviously you can just use a dictionary. Not the point of the questions, thanks.