Questions tagged [keypaths]

A key path is a representation of a sequence of key names specifying a path to a nested value. For example, a path to a value in a JSON document could be specified as an array of strings.

59 questions
47
votes
2 answers

Swift 4 approach for observeValue(forKeyPath:...)

I've been trying to find an example, but what I've seen doesn't work in my case. What would be the equivalent of the following code: object.addObserver(self, forKeyPath: "keyPath", options: [.new], context: nil) override public func observeValue( …
MVZ
  • 2,220
  • 5
  • 27
  • 57
17
votes
3 answers

Swift Generics, Constraints, and KeyPaths

I'm aware of the limitations of generics in Swift and why they exist so this is not a question about compiler errors. Rather, I occasionally run into situations that seem as though they should be possible with some combination of the resources…
WCByrne
  • 1,549
  • 1
  • 11
  • 16
14
votes
5 answers

Convert delimited string into array key path and assign value

I have a string like this: $string = 'one/two/three/four'; which I turn it into a array: $keys = explode('/', $string); This array can have any number of elements, like 1, 2, 5 etc. How can I assign a certain value to a multidimensional array, but…
Alex
  • 66,732
  • 177
  • 439
  • 641
8
votes
1 answer

How to use heterogeneous array of Swift KeyPaths

It seems like it should be possible to use an array of KeyPaths as sort keys to sort an array of Swift structs using an arbitrary number of sort keys. Conceptually, it's simple. You define an array of KeyPaths to a generic object, where the only…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
8
votes
2 answers

Can the non-string "property name" passed to #keyPath() be saved independently?

I was very happy to find Swift 3's implementation of #keyPath(), which will eliminate typos and enforce at compile time that the key path actually exists. Much better than manually typing…
pkamb
  • 33,281
  • 23
  • 160
  • 191
7
votes
2 answers

Strange error with KeyPath on optional value

Consider the following snippet of code class A { var value: Int? } let a: A? = A() let kp = \A.value a?[keyPath: kp] = 10 print(a?.value) This works perfectly and Optional(10) is printed as expected. In my actual app the field I'm…
Marco Boschi
  • 2,321
  • 1
  • 16
  • 30
6
votes
2 answers

Trouble using swift 4's KVO "observe" instead of addObserver

I'm having some trouble getting the new KVO syntax right. According to the Apple documentation: Create an observer for the key path and call the observe(_:options:changeHandler) method. For more information on key paths, see Keys and Key…
Eden
  • 1,782
  • 15
  • 23
6
votes
2 answers

How do You Convert an AnyKeyPath to a WritableKeyPath?

I have an array of enum cases, where each case has a keyPath property, which returns an AnyKeyPath matching the classes property with the same name as the enum case: protocol PathAccessor: CodingKey { var keyPath: AnyKeyPath { get } static…
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
5
votes
2 answers

How to define an empty, "uninitialized" keypath and set it later?

I made a program to calculate the total width/height of views (sometimes I want the total width, sometimes I want the total height). The only catch is: If I'm calculating the width, I want to add an extra 10 to the total. Here's my current…
aheze
  • 24,434
  • 8
  • 68
  • 125
4
votes
1 answer

Adding observer to AVPlayer causes a crash with no printout in console

I am trying to add an observer to my AVPlayer in swift that allows reads when the video begins playing to control other functions on my player. When I run the following code: func setUpPlayer() { if let url = URL(string: urlString) { …
4
votes
1 answer

Swift Keypath from String

Is there a way to create a Keypath from a String in Swift 4 to access a value in a struct by its path or variable name Finally I found out that I should use CodingKeys instead of KeyPaths to access the a value of a variable of a struct by…
Michael_mhr
  • 744
  • 1
  • 9
  • 27
3
votes
2 answers

map(keyPath) where keyPath is a variable

let arr = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] arr.map(\.0) // [1, 2, 3, 4, 5] Works great. But the below code doesn't compile: let keyPath = \(Int, Int).0 arr.map(keyPath) Cannot convert value of type 'WritableKeyPath<(Int, Int), Int>'…
Roman
  • 1,309
  • 14
  • 23
3
votes
1 answer

Swift Keypath with UIButtons

I'm trying to get a keypath to the selected property of an IBOutlet within a class. But get: Type 'UIButton?' has no member 'isSelected' directly accessing the UIButton.isSelected keypath works, but doesn't fulfill my usecase. @objc class Demo:…
Jan
  • 1,827
  • 2
  • 16
  • 29
3
votes
2 answers

identified(by: \.self) - what does it do?

In this video: https://developer.apple.com/videos/play/wwdc2019/103/, the following snippet of code is shown at around 15:30: ... ForEach(ContentSizeCategory.common.identified(by: \.self)) ... What does it do? Where does self points to? The current…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
3
votes
1 answer

Swift keyPath vs protocol

I understand the basic idea of keyPaths but I do not understand its use cases. If you already know the type of the instance, you can access their properties easily. If you don’t, protocol already supports read-only, read-write properties. Can…
Tung Vo Duc
  • 503
  • 6
  • 16
1
2 3 4