Use this code as a reference.
If you comment out the code and leave the contents empty, and then run it, the same behavior will occur.
struct SizePreferenceKey: PreferenceKey {
typealias Value = CGSize
static var defaultValue: Value = .zero
static func reduce(value _: inout Value, nextValue: () -> Value) {
// _ = nextValue() <-- comment out
}
}
I tried the same thing to this code, and it works fine.
struct ViewOffsetKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
// value += nextValue() <-- comment out
}
}
I have to write it to conform
the protocol
, but it seems to work even if the content is empty. Why is this? When do I need the content?
In other words, it seems that onPreferenceChange
is working fine without updating the value
. Why is this?