Questions tagged [swift-keypath]

43 questions
0
votes
0 answers

Downcasting PartialKeyPath to ReferenceWriteableKeyPath using generics?

I want a simple serialization implementation that stores values of various KeyPaths for a class and applies them. I know this can be implemented by storing vars manually, but I'd prefer to use KeyPaths since it would be easier to add additional…
0
votes
3 answers

In Swift, how can I implement a generic system that processes a type by registering processors for different key paths?

I want to implement a system that allows me to generically process a type by registering processors for different key paths. One trait of the system should be composition, so every processor should extend one common generic protocol…
alexcristea
  • 2,316
  • 16
  • 18
0
votes
1 answer

Get the coding key for a keypath in Swift

What I have: A codable struct with different properties. What I want: A function, where I can get the exact name of the property when it is encoded in a Json. I think the most promising approach is using Keypath, but I have no idea how and whether…
Nicolas Degen
  • 1,522
  • 2
  • 15
  • 24
0
votes
1 answer

Using a protocol to define a collection of KeyPaths with different values

Suppose you want to keep track of one or more unique identifier(s). In this case, A has two properties that are considered unique to A, while B has only one property that is unique to B. protocol HavingUID { // Some way to use KeyPath? } struct…
GoldenJoe
  • 7,874
  • 7
  • 53
  • 92
0
votes
0 answers

Shorthand key path in switch statement?

I have a method that takes a key path for a type, however it is giving me a compile error when I try to use the shorthand version of the key path. Here's what I'm trying to do: struct MyType { let prop1: String let prop2: Int let prop3:…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
0
votes
2 answers

Specify Key Path in a List Swift UI

I have below structure , where GroceryData has details about the section as [GrocerySection] , this in turn has items to be displayed in the section as [Grocery]. struct GroceryData { var showFavorites:Bool = false var…
NNikN
  • 3,720
  • 6
  • 44
  • 86
0
votes
2 answers

Test if type pointed to by KeyPath is a Set

Given a KeyPath, is there a clean way to test if Value.self is a Set? For Core Data, I want to specify an API that takes a KeyPath, and can use this to determine if it's a toMany relationship. My best attempt thus far was…
Procrastin8
  • 4,193
  • 12
  • 25
0
votes
1 answer

How can I update this class to support binding a keypath value to a function?

I have a class that allows me to declare a property as Bindable let user: Bindable = Bindable(someUser) user.update(with: someNewUser) ...... user.bind(\.name, to: label, \.text) Doing so allows changes to be bound to a UI element…
Harry Blue
  • 4,202
  • 10
  • 39
  • 78
0
votes
1 answer

Set value of array via KeyPath

I am trying to update the value stored in an array property of a class via the use of KeyPaths. Here is my code: func listenAndUpdateDocuments( _ property: ReferenceWritableKeyPath, from model: MyModel) { …
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
0
votes
0 answers

What does "__lldb_expr_18" mean?

I'm working with a functional swift library in a playground and doing some manipulations with keypaths. When I run: prop(\User.favoriteFoods) <<< map <<< prop(.name) I get: ((String) -> String) -> (__lldb_expr_18.User) -> __lldb_expr_18.User The…
0
votes
1 answer

How do I fix error: Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

I am going through old code from HomeKit Catalog: Creating Homes, Pairing and Controlling Accessories, and Setting Up Triggers when I ran into an expression that says .KeyPathExpressionType I can't tell what the . in .KeyPathExpressionType is…
daniel
  • 1,446
  • 3
  • 29
  • 65
0
votes
1 answer

Swift passing key path as a param of func

I need to be able to pass #keypath to a func to filter my CoreData records. I do smth like this: func filteredExercises(with propertyKeyPath: #keyPath, filter: Any) { do { let filteredExercises = try CoreStore.fetchAll( …
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
-1
votes
1 answer

Swift: .contains(where:) using key-path expression

Is there someway to use a key-path expression to simplify boilerplate code in a Swift contains(where:)? E.g. struct Struct { let bool: Bool } let structs = [ Struct(bool: false), Struct(bool: false), Struct(bool: true), Struct(bool:…
Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
1 2
3