Questions tagged [swift-keypath]
43 questions
15
votes
1 answer
How does Swift ReferenceWritableKeyPath work with an Optional property?
Ground of Being: It will help, before reading, to know that you cannot assign a UIImage to an image view outlet's image property through the keypath \UIImageView.image. Here's the property:
@IBOutlet weak var iv: UIImageView!
Now, will this…

matt
- 515,959
- 87
- 875
- 1,141
8
votes
2 answers
Fatal error: could not demangle keypath type
I have a simple class and I want to use keypath in the init, something like this:
class V: UIView {
convenience init() {
self.init(frame: .zero)
self[keyPath: \.alpha] = 0.5
}
}
let v = View()
When I run this code I get a…

Anton Belousov
- 1,140
- 15
- 34
4
votes
1 answer
Apply KeyPath in map() in simplest form
let test = [4, 5, 3, 1, 3]
print(
test.map { $0 }
)
print(
test.map(\.self) // Doesn't compile
)
Error:
Type of expression is ambiguous without more context
Why doesn't it work? Seems like it should.
If isn't this way, how else can we…

Roman
- 1,309
- 14
- 23
4
votes
1 answer
Advantages of KVC
I have been trying to figure this out for a while but cannot understand the advantage of KVC other than :
Compiler checks (thus avoiding stringly typed code)
Used with KVO
I am not sure if there is any advantage of using KVC other than the 2 cases…

user121095
- 697
- 2
- 6
- 18
4
votes
1 answer
Observing a list of Swift KeyPaths
I have the following code which adds an observer on two properties:
obs = []
let xa = self.observe(\CirclePolygon.radius, options: [.new]) { (op, ch) in
callback()
}
obs.append(xa)
let xb =…

Chris
- 2,739
- 4
- 29
- 57
3
votes
2 answers
Why can't I use key path syntax in a map statement?
Imagine I have a struct AStruct:
struct AStruct {
let name: String
let value: Int
}
And imagine I have an array of AStructs:
let array: [AStruct] = [
AStruct(name: "John", value: 1),
AStruct(name: "Bob", value: 23),
AStruct(name:…

Duncan C
- 128,072
- 22
- 173
- 272
3
votes
0 answers
Redundant duplication of typealiase declarations when conforms to a protocol
protocol PathCollection: Collection where Element == Target.Element, Index == Target.Index {
associatedtype Target: Collection
static var reference: KeyPath { get }
}
extension PathCollection {
private var target: Target {…

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
1 answer
INIntent `setImage` make runtime crash in Swift 5
I've used INIntent object since Siri shortcut is added. For that I made an intent definition and it generated a INIntent object automatically.
@available(iOS 12.0, watchOS 5.0, *)
@objc(SpotConditionIntent)
public class SpotConditionIntent: INIntent…

Ryan
- 4,799
- 1
- 29
- 56
2
votes
4 answers
SwiftUI - Automatically add dividers between each element of a `ForEach`
I'm using a ForEach to display the contents of an array, then manually showing a divider between each element by checking the element index. Here's my code:
struct ContentView: View {
let animals = ["Apple", "Bear", "Cat", "Dog", "Elephant"]
…

aheze
- 24,434
- 8
- 68
- 125
2
votes
1 answer
Key path value type 'Int' cannot be converted to contextual type 'String'
I am trying to pass a multiple tuples containing a KeyPath and a type of a sort order to a method that should do sorting.
I have this method:
extension Array {
mutating func sort(by criteria: (path: KeyPath,…

Whirlwind
- 14,286
- 11
- 68
- 157
2
votes
2 answers
Storing KeyPaths in Dictionary/HashMap
I'm facing a problem when hashing a ReferenceWritableKeyPath. It appears that the hash function also takes the generic properties of the ReferenceWritableKeyPath into account when hashing the key path. I've included sample code to show why this is a…

CentrumGuy
- 576
- 1
- 4
- 16
2
votes
1 answer
Unwrapping Optional types in a Generic method in Swift 5
I'm trying to create an instance of type B from an instance of type A, however some of the properties of the original type A are optional and the creation of B should throw an error if that occurs.
My issue is I can't tell if type T is optional, and…

Stephen Furlani
- 6,794
- 4
- 31
- 60
2
votes
0 answers
More about the weird behavior of ReferenceWritableKeyPath with an Optional property
(I say "more" in my title because this question is an appendix to my How does Swift ReferenceWritableKeyPath work with an Optional property?.)
In my code, self.iv is an outlet property:
@IBOutlet weak var iv: UIImageView!
Now, will this compile?
…

matt
- 515,959
- 87
- 875
- 1,141
2
votes
1 answer
Assign dynamically properties of a Struct in Swift
I have this Struct:
struct Alphabet {
let a = "ciao"
let b = "hi"
let c = "hola"
}
let alphabet = Alphabet()
I want the value of each property become the string of the property itself.
Like this:
alphabet.a = "a"
alphabet.b =…

Lorenzo Fiamingo
- 3,251
- 2
- 17
- 35