RealmSwift.Objectに以下の定義があります。
Swift
1/**
2Override this method to specify the name of a property to be used as the primary key.
3
4Only properties of types `String` and `Int` can be designated as the primary key. Primary key properties enforce
5uniqueness for each value whenever the property is set, which incurs minor overhead. Indexes are created
6automatically for primary key properties.
7
8- returns: The name of the property designated as the primary key, or `nil` if the model has no primary key.
9 */
10 @objc open class func primaryKey() -> String? { return nil }
primaryKey()は、Realmライブラリがプライマリキーに使用するプロパティの名前を取得するためのfuncです。
従って、名前(String)しか返せません。
また、実際にプライマリキーとして使用できるのは、String
型かInt
型のプロパティなので、Date
型のプロパティ名を指定すると、実行時にRealmライブラリ内で処理ができずに落ちます。