3

After reading the documentation I was not able to know how can I create a table with primary key integer with autoincrement attribute. Im using swift.

import UIKit
import Realm

class Person: RLMObject {
    var name = ""
    var birthdate = NSDate(timeIntervalSince1970: 1)
    var dogs = RLMArray(objectClassName: Dog.className())
}

Thanks In Advance

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
Xing
  • 53
  • 1
  • 4

1 Answers1

0

I think to set a primary key (assuming you are ok with strings) you should use:

class Person: RLMObject {
    var id = ""
    var name = ""
    var birthdate = NSDate(timeIntervalSince1970: 1)
    var dogs = RLMArray(objectClassName: Dog.className())

    override class func primaryKey() -> String {
        return "id"
    }
}

Then you need to use unique id's when you set the Person.id. Here is a related question that discusses that How do I set a auto increment key in Realm?

Community
  • 1
  • 1
yoshyosh
  • 13,956
  • 14
  • 38
  • 46