swift - Converting RealmSwift.List model object into structure -
the topic might not understable explain want achieve.
the exapmle json this:
{ "name": "somename", "addresses": [ { "city": "berlin", "street": "dunno" }, { "city": "paris", "street": "dunno2" } ] }
the realm
objects :
public final class placeobject: object { @objc dynamic var name = "" var addresses = realmswift.list<address>() override public static func primarykey() -> string? { return "name" // it's wrong primary key, should id / auto incremenet id, example might this. } } public final class addressobject: object { @objc dynamic var city = "" @objc dynamic var street = "" override public static func primarykey() -> string? { return "city" // same above. } }
and following amazing tutorial switching classes structrues using realm (protocols included there)
i've achived want except saving 1 object nested dictionaries / arrays.
the structures looks :
struct place { public let name: string public let addresses: [address] } extension place : persistable { public init(managedobject: managedobject) { name = managedobject.name addresses = managedobject.addresses } public func managedobject() -> placeobject { let placeobj = placeobject() placeobj.name = name placeobj.addresses = addresses return placeobj } public enum propertyvalue : propertyvaluetype { case name(string) case addresses([address]) public var propertyvaluepair: propertyvaluepair { switch self { case .name(let id): return ("name", id) case .addresses(let id): return ("addresses", id) } } } public enum query: querytype { case name(string) public var predicate: nspredicate? { switch self { case .name(let value): return nspredicate(format: "name == %@", value) } } public var sortdescriptors: [sortdescriptor] { return [sortdescriptor(keypath: "name")] } } } struct address { public let city: string public let street: string } extension address : persistable { public init(managedobject: managedobject) { city = managedobject.city address = managedobject.address } public func managedobject() -> addressobject { let addreses = addressobject() addreses.city = city addreses.street = street return addreses } public enum propertyvalue : propertyvaluetype { case city(string) case street(string) public var propertyvaluepair: propertyvaluepair { switch self { case .city(let id): return ("city", id) case .street(let id): return ("street", id) } } } public enum query: querytype { case city(string) public var predicate: nspredicate? { switch self { case .city(let value): return nspredicate(format: "city == %@", value) } } public var sortdescriptors: [sortdescriptor] { return [sortdescriptor(keypath: "city")] } } }
i expected errors:
error: cannot assign value of type 'list<addressobject>' type '[address]' addresses = managedobject.addresses error: cannot assign value of type '[address]' type 'list<addressobject>' placeobj.addresses = addresses
my question how convert type [address]
list<addressobject>
, in different way match pattern ?
work around
there way in messy style. don't save nasted array inside placeobject
create aditional key object , via key connect these 2 while retriving them data base. in opinion it's not nice way - redundant logic.
thanks in advance!
Comments
Post a Comment