How to add new property to a Realm Object?

Two ways to do it:

  1. Just delete your app from simulator and run it again. Everytime you change properties on your Realm objects your existing database becomes incompatible to the new one. As long as you are still in the developing stage you can simply delete the app from the simulator / device and start it again.

  2. Write this code in AppDelegate's disFinishLaunchWithOptions method:

    let config = Realm.Configuration( schemaVersion: 1, migrationBlock: { migration, oldSchemaVersion in if (oldSchemaVersion < 1) { // Nothing to do! // Realm will automatically detect new properties and removed properties // And will update the schema on disk automatically } }) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()

I suggest you to follow the second one.


You just add the property to the Realm model, but will have to provide a migration in order to update the stored data to the new format.

This includes setting a schemaVersion in your Realm.Configuration which tells Realm that the schema was changed, and providing a migrationBlock which initializes the new property for existing objects (probably just by setting an empty string). On the next start of the app, Realm will automatically run the migration and thereby update the stored data to the new schema.