diff --git a/CHANGELOG.md b/CHANGELOG.md index 44e500bd88..4fccc5ceb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ x.y.z Release notes (yyyy-MM-dd) ============================================================= ### Enhancements -* None. +* Add support for KeyPath initializiation for LinkingObjects declared with @Persisted: + `@Persisted(originProperty: \.dogs) var persons: LinkingObjects ### Fixed * ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?) diff --git a/RealmSwift/PersistedProperty.swift b/RealmSwift/PersistedProperty.swift index d094fff055..bb41855886 100644 --- a/RealmSwift/PersistedProperty.swift +++ b/RealmSwift/PersistedProperty.swift @@ -361,6 +361,9 @@ extension Persisted where Value: LinkingObjectsProtocol { public init(originProperty: String) { self.init(wrappedValue: Value(fromType: Value.Element.self, property: originProperty)) } + public init(originProperty: KeyPath) where Value.Element: ObjectBase, V: ObjectBase { + self.init(wrappedValue: Value(fromType: Value.Element.self, property: _name(for: originProperty))) + } } extension LinkingObjects: LinkingObjectsProtocol {} diff --git a/RealmSwift/Tests/ModernTestObjects.swift b/RealmSwift/Tests/ModernTestObjects.swift index b2729c5802..7407cbf0ba 100644 --- a/RealmSwift/Tests/ModernTestObjects.swift +++ b/RealmSwift/Tests/ModernTestObjects.swift @@ -156,7 +156,7 @@ class ModernAllTypesObject: Object { @Persisted var mapOptObjectId: Map @Persisted var mapOptUuid: Map - @Persisted(originProperty: "objectCol") + @Persisted(originProperty: \.objectCol) var linkingObjects: LinkingObjects }