I want make a Generic PreferenceKey, which I got 2 issues with my codes.
First I need define () for T, for using normal type like String, Int as String() or Int() so I need memberwise initializer for T.
Second Xcode complain that my PreferenceKey does not conform to Equatable, while I did it! <T: Equatable>
How can I solve this 2 issues? thanks
struct ContentView: View {
@State private var stringOfText: String = "Hello, world!"
var body: some View {
Text(stringOfText)
.preference(key: CustomPreferenceKey.self, value: stringOfText)
.onPreferenceChange(CustomPreferenceKey.self) { newValue in print(newValue) }
}
}
struct CustomPreferenceKey<T: Equatable>: PreferenceKey {
static var defaultValue: T { get { return T() } }
static func reduce(value: inout T, nextValue: () -> T) { value = nextValue() }
}