Questions tagged [swift-keypath]

43 questions
1
vote
0 answers

Map enum to KeyPath to access properties in a generic way

I am trying to map enum values to properties on a struct, so the properties can be accessed by enum, and the properties can be iterated and processed in a generic way. Here is a contrived example, which demonstrates the compiler errors I'm…
Nick
  • 3,958
  • 4
  • 32
  • 47
1
vote
0 answers

Swift init via KeyPath / WritableKeyPath

Is there any way to initialize a Swift type by setting its properties via Key Path? struct SomeStruct { var number: Int init(to number: Int) { self.number = number // ok } init(viaKeyPathTo number: Int) { …
pkamb
  • 33,281
  • 23
  • 160
  • 191
1
vote
1 answer

Swift - Mutate values of enum associate type properties

I have a 2 structs conforming to one protocol. I created enum with associated types as struct types. When I try to access properties of protocol type A using dynamicmember lookup, getters are working fine. But Set methods are throwing error: Key…
1
vote
1 answer

Convert an optional Type to non-optional Type

The following code throws a compile error: import UIKit class Initable { required init() {} } class NestedObject:Initable { var nestedProp:String? = nil } class MyObj { var name:String? = nil var value:Int? = nil var…
steventnorris
  • 5,656
  • 23
  • 93
  • 174
1
vote
1 answer

Storing array of heterogeneous keypaths

I have an object similar to: class MyObject { var title:String? = nil var value:Int? = nil var otherData:[String] = [] } I am attempting to store a heterogeneous array of updates so I can use them later to set values. Something like…
steventnorris
  • 5,656
  • 23
  • 93
  • 174
1
vote
0 answers

How can I use NSPredicate with a computed property in Swift?

I am trying to create a @FetchRequest in SwiftUI using an NSPredicate, like so: @FetchRequest(entity: CoreDataRecipe.entity(), sortDescriptors: [], predicate: NSPredicate(format: "isRunning == true")) var runningRecipes:…
bob_mosh
  • 267
  • 2
  • 17
1
vote
1 answer

Key path value type 'Data.Element.ID' cannot be converted to contextual type 'ID'

I am getting the error Key path value type 'Data.Element.ID' cannot be converted to contextual type 'ID' when trying to create the initializer in the extension below for when the data which is passed contains elements which already inherit from…
Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
1
vote
0 answers

Generic NSKeyValueObservedChange/Keypath

I have an array of NSKeyValueObservation. I am watching a few properties on a NSManagedObject and I want to execute all of the same method when a property changed. This is the code: let completionHandler: ((MyObject, NSKeyValueObservedChange)…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
1
vote
0 answers

Using KeyPaths on associatedtype objects

I am making a backend using Vapor 3 and Swift 5. On some part I'm trying to use a KeyPath on an associatedtype like so: protocol MyProtocol { associatedType T: Model & Parameter // Some more declarations } extension MyProtocol where T.ID:…
lsauceda
  • 315
  • 3
  • 12
1
vote
0 answers

Accessing subclass members using KeyPath

I was trying to access subclass members using KeyPath when I encountered something strange. Look at these 2 simple classes: class A { var a:String { get { return "str" } } } class B: A { override var a:String…
Afshin
  • 8,839
  • 1
  • 18
  • 53
0
votes
0 answers

How to cast KeyPath> to KeyPath

I need to put KeyPath values in an array of type Array>. However, \CNContact.phoneNumbers fails to cast to KeyPath. Here is my test code: let keyPath0: KeyPath = \CNContact.phoneNumbers // Runtime…
daniel
  • 1,446
  • 3
  • 29
  • 65
0
votes
0 answers

How can I sort dates in swift realm repository using my own comparator?

Context: I saved a list of objects in realm repository. These objects contain a property of standard type Date. I would like to get a list of those objects back from Realm based on distinct dates which means in other words : If two objects in realm…
Red
  • 1
0
votes
1 answer

SwiftUI Can't Pass String to KeyPath

I have an app that references a large dataset that I load from an external public site as a comma separated value file. I parse the data to a an array of a model called WaterPointModel. An abbreviated version is: struct WaterPointModel: Identifiable…
JohnSF
  • 3,736
  • 3
  • 36
  • 72
0
votes
0 answers

Get coding value for a Codable value in Swift

I am working on a Swift service that stores codable types in JSON. The type also defines a nested CodingKeys enum to define the property names when coded to JSON. For example, I might have a type that looks like this: struct MyType : Codable { let…
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
0
votes
1 answer

how do you write a KeyPath to reference the value of a dictionary

Say I have a struct like this: struct Person { var kids: [KidId: KidName] = [:] } Is it possible to construct a keypath to reference the value of something in the User.kids property? Something like \User.kids[KidId]? Specifically, I'd like to…
Jared Henderson
  • 1,299
  • 3
  • 13
  • 21