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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RealmSwift/PersistedProperty.swift
Original file line number Diff line number Diff line change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This'll allow specifying any type of property as the origin property and ideally it'd be constrained to links.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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
Original file line number Diff line number Diff line change
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