Skip to content

Instantly share code, notes, and snippets.

@godrm
Last active August 19, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godrm/24ce6b64c3944da2074a020de84a9048 to your computer and use it in GitHub Desktop.
Save godrm/24ce6b64c3944da2074a020de84a9048 to your computer and use it in GitHub Desktop.
class Animal : Codable {
var type : String {
return "animal"
}
}
class Dog : Animal {
override var type : String {
return "dog"
}
}
class Cat : Animal {
override var type : String {
return "cat"
}
}
var array : [Animal] = [Dog(), Cat(), Dog(), Cat()];
array.forEach{ print($0.type) }
var data = try PropertyListEncoder().encode(array)
var restoreArray = try? PropertyListDecoder().decode(Array<Animal>.self, from: data)
restoreArray?.forEach{ print($0.type) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment