Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- struct Codables {
- struct Insect: Codable {
- let name: String
- }
- struct Bee: Insect {
- let wingCount: Int
- }
- struct Beetle: Insect {
- let hasHorn: Bool
- }
- func work() {
- let bee = Bee(name: "Maya", wingCount: 4)
- let beetle = Beetle(name: "Juice", hasHorn: false)
- let encoder = JSONEncoder()
- let insects: [Insect] = [bee, beetle]
- let data = try! encoder.encode(insects)
- let restoredInsects = try! JSONDecoder().decode([Insect].self, from: data)
- print(restoredInsects)
- // Insect(name: "Maya")
- // Insect(name: "Beetle")
- // no wings/horn info was decoded, however data contains it
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement