Type 'User' does not conform to protocol 'RealmRepresentable'
ranhsd opened this issue · comments
Hi,
I've created a User entity in my domain.
My user entity looks like the following:
public struct User {
public let username : String?
public let email : String?
public let uid: String?
public let firstName: String?
public let lastName: String?
public let password: String?
public let createdAt: Date?
public let updatedAt: Date?
public init(builder: UserBuilder) {
self.username = builder.username
self.firstName = builder.firstName
self.lastName = builder.lastName
self.uid = builder.userId
self.password = nil
self.email = builder.email
self.createdAt = builder.createdAt
self.updatedAt = builder.updatedAt
}
}
extension User : Equatable {
public static func == (lhs: User, rhs: User) -> Bool {
return lhs.uid == rhs.uid &&
lhs.firstName == rhs.firstName &&
lhs.lastName == rhs.lastName &&
lhs.createdAt == rhs.createdAt &&
lhs.updatedAt == rhs.updatedAt
}
}
Then, i started to build the Realm platform to use as offline database. There I've create the RealmUser entity and it looks like the following:
import Realm
import RealmSwift
import Domain
import QueryKit
final class RealmUser : Object {
dynamic var uid : String = ""
dynamic var email : String = ""
dynamic var firstName : String = ""
dynamic var lastName : String = ""
override class func primaryKey() -> String? {
return "uid"
}
}
extension RealmUser {
static var uid : Attribute<String> { return Attribute("uid") }
static var email : Attribute<String> { return Attribute("email") }
static var firstName : Attribute<String> { return Attribute("firstName") }
static var lastName : Attribute<String> { return Attribute("lastName") }
}
extension RealmUser : DomainConvertibleType {
func asDomain() -> User {
var builder = UserBuilder()
builder.userId = uid
builder.email = email
builder.firstName = firstName
builder.lastName = lastName
let user = User(builder: builder)
return user
}
}
extension User : RealmRepresentable {
func asRealm() -> RealmUser {
return RealmUser.build { object in
object.uid = uid!
object.email = email!
object.firstName = firstName!
object.lastName = lastName!
}
}
}
My issue is in this specific line:
extension User : RealmRepresentable
swift compiler yelling me that the:
Type 'User' does not conform to protocol 'RealmRepresentable'
I know that i can implement the
var uid: String {get}
protocol, but i looked at your code and you are not doing it and it still work for you.
Your code looks like the following:
extension User: RealmRepresentable {
func asRealm() -> RMUser {
return RMUser.build { object in
object.uid = uid
object.address = address.asRealm()
object.company = company.asRealm()
object.email = email
object.name = name
object.phone = phone
object.username = username
object.website = website
}
}
}
as you can see, you are not overriding the uid property and it still works for you...
am i missing something?
Thanks!
ok , forget it i found it.
My uid property was optional. soon as i changed it to not optional it worked.