Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyPath initializer for Persisted<LinkingObjects> #8136

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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<Person>

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?)
Expand Down
3 changes: 3 additions & 0 deletions RealmSwift/PersistedProperty.swift
Expand Up @@ -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<V>(originProperty: KeyPath<Value.Element, V?>) where Value.Element: ObjectBase, V: ObjectBase {
self.init(wrappedValue: Value(fromType: Value.Element.self, property: _name(for: originProperty)))
}
}
extension LinkingObjects: LinkingObjectsProtocol {}

Expand Down
2 changes: 1 addition & 1 deletion RealmSwift/Tests/ModernTestObjects.swift
Expand Up @@ -156,7 +156,7 @@ class ModernAllTypesObject: Object {
@Persisted var mapOptObjectId: Map<String, ObjectId?>
@Persisted var mapOptUuid: Map<String, UUID?>

@Persisted(originProperty: "objectCol")
@Persisted(originProperty: \.objectCol)
var linkingObjects: LinkingObjects<ModernAllTypesObject>
}

Expand Down