Swift, Codable, Codables , . , , CodingKeys , , Codable, .
, Swift structs ( "memberwise initializer" ), . Swift, , / , , .
, Sourcery (https://github.com/krzysztofzablocki/Sourcery), . Sourcery script , Command
, CommandHandler
.
, , , Codable Swift, . Swift, .. , , script, .
, CommandIds, , , CommandId
:
protocol CommandId {
static var all: [Self] { get }
}
:
class HandlerA : CommandHandler {
enum CommandIds : String, CommandId {
case commandA1
case commandA2
static var all: [CommandIds] { return [.commandA1, .commandA2] }
}
}
:
func processHandler<T:CommandHandler>(_ handler:T){
T.CommandIds.all.forEach { // Do something with each command case }
}
, , Codable Swift - . Codable-matchinging init(from decoder: Decoder)
, , ,
struct Example: Codable {
let name: String
let number: Int
}
extension Example {
enum CodingKeys: String, CodingKey {
case name, number
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
number = try values.decode(Int.self, forKey: .number)
}
}
Swift - , , (), .
, , Swift . , Core Swift Apple , , WWDC.
, , Swift pull, Swift (, Codable Equatable Hashable), Sourcery, Swift.