Search code examples
iosswiftrealm

Fatal error: Make sure you've registered type 'User': file


I am trying to use Realm for data persistence in my first iOS application. I decided to use Realmable from Unrealm since I am using struct instead of class as my entity model but I get this error Fatal error: Make sure you've registered type 'User': file when I tried to add to the database.

How can I get through this?

Code


    import UIKit
    import Unrealm
    import RealmSwift
    
    struct User:Encodable, Realmable {
        init() {
            
        }
        
        var name:String?
        var email:String?
        var password:String?
        var country:String?
        var phone:String?
        var token:String?
        var changedPassword:Bool?
    
      
        static func primaryKey() -> String? {
            return "email"
        }
    }
    
            let realm = try! Realm()
            var user1 = User()
                      user1.name = "Tade"
                      user1.email = "[email protected]"
                      user1.password = "payload.password"
                      user1.country = "payload.country"
                      user1.phone = "payload.phone"
                      user1.changedPassword = false
                      user1.token = "AuthResponse.token"
                      
                      do{
                          try! realm.write{
                              realm.add(user1)
                          }
                      }catch{
                          print(error)
                      }


Solution

  • You have to Register your Classes/Structs and Enums in AppDelegate's didFinishLaunchingWithOptions "Before using them"

    Realm.registerRealmables(User.self)