I have an array of struct 'Pair' (Data) and I want to change the element in the array if some condition is met. I have followed the documentation https://docs.swift.org/swift-book/LanguageGuide/Methods.html and created a 'setValue' method with mutating
But when I debug the code, I see the value of the pair variable get changed. But the array 'data?.pairs' does not get changed to the new value 'Point(1,2). Can you please tell what am I missing?
public struct Data {
var pairs: [Pair<Point>]
}
var array = data?.pairs ?? []
for index in 0..<array.count {
var pair = data?.pairs[index]
if /* some test */ {
print ("Found match")
pair?.setValue(Point(1,2)) // `pair` is updated, but data?.paris[index] is not updated.
}
}
struct Pair<Value: Comparable>{
var key: Value
var value: Value
init(_ key: Value, _ value: Value) {
self.key = key
self.value = value
}
mutating func setValue(_ value: Value) {
self.value = value
}
}
Datais a native Swift type (Foundation).