, К сожалению, для каждого подкласса вам нужно самостоятельно реализовать кодирование и декодирование свойств.

использование наследования классов нарушает Декодируемость класса. Например, следующий код

class Server : Codable {
    var id : Int?
}

class Development : Server {
    var name : String?
    var userId : Int?
}

var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}"
let jsonDecoder = JSONDecoder()
let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development

print(item.id ?? "id is nil")
print(item.name ?? "name is nil") here

вывод:

1
name is nil

Теперь, если я переверну это, имя расшифровывается, а id - нет.

class Server {
    var id : Int?
}

class Development : Server, Codable {
    var name : String?
    var userId : Int?
}

var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}"
let jsonDecoder = JSONDecoder()
let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development

print(item.id ?? "id is nil")
print(item.name ?? "name is nil")

вывод:

id is nil
Large Building Development

И вы не можете выразить Codable в обоих классах.

Ответы на вопрос(5)

Ваш ответ на вопрос