Last active
December 30, 2017 00:04
-
-
Save kmkrn/b818598c74c4f76f34a451b1700164b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Realm | |
import RealmSwift | |
class Cat: Object, Decodable { | |
@objc dynamic var catId: Int = 0 | |
@objc dynamic var name: String = "" | |
@objc dynamic var breed: String = "" | |
@objc dynamic var weight: Double = 0 | |
override static func primaryKey() -> String? { | |
return "catId" | |
} | |
private enum CatCodingKeys: String, CodingKey { | |
case catId | |
case name | |
case breed | |
case weight | |
} | |
convenience init(catId: Int, name: String, breed: String, weight: Double) { | |
self.init() | |
self.catId = catId | |
self.name = name | |
self.breed = breed | |
self.weight = weight | |
} | |
convenience required init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: CatCodingKeys.self) | |
let catId = try container.decode(Int.self, forKey: .catId) | |
let name = try container.decode(String.self, forKey: .name) | |
let breed = try container.decode(String.self, forKey: .breed) | |
let weight = try container.decode(Double.self, forKey: .weight) | |
self.init(catId: catId, name: name, breed: breed, weight: weight) | |
} | |
required init() { | |
super.init() | |
} | |
required init(value: Any, schema: RLMSchema) { | |
super.init(value: value, schema: schema) | |
} | |
required init(realm: RLMRealm, schema: RLMObjectSchema) { | |
super.init(realm: realm, schema: schema) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment