protocol AlignmentDescriptor {
associatedtype T: Equatable
static var alignments: [T] { get }
static func name(of alignment: T) -> String
}
struct HorizontalAlignmentDescriptor: AlignmentDescriptor {
static let alignments = [HorizontalAlignment.leading, .center, .trailing]
static func name(of alignment: HorizontalAlignment) -> String {
switch alignment {
case .leading:
return "Leading"
case .center:
return "Center"
case .trailing:
return "Trailing"
default:
fatalError("Got some unknown alignment value!!!!")
}
}
}
struct VerticalAlignmentDescriptor: AlignmentDescriptor {
static let alignments = [VerticalAlignment.top, .center, .bottom]
static func name(of alignment: VerticalAlignment) -> String {
switch alignment {
case .top:
return "Top"
case .center:
return "Center"
case .bottom:
return "Bottom"
default:
fatalError("Got some unknown alignment value!!!!")
}
}
}
struct AlignmentPicker<AD: AlignmentDescriptor>: View {
@Binding var alignment: AD.T
var body: some View {
Picker(selection: Binding<Int>(get: { AD.alignments.firstIndex(of: self.alignment)! }, set: { self.alignment = AD.alignments[$0] }),
label: Text("Choose your alignment")) {
ForEach(0..<AD.alignments.count) { index in
Text(AD.name(of: AD.alignments[index])).tag(index)
}
}.pickerStyle(SegmentedPickerStyle())
}
}
But got error:
struct ImplicitAlignmentDemo: View {
@State private var alignment: HorizontalAlignment = .trailing
var body: some View {
// Compile error at self.$alignment
// Cannot convert value of type 'Binding<HorizontalAlignment>' to expected argument type 'Binding<_>'
// 1. Arguments to generic parameter 'Value' ('HorizontalAlignment' and '_') are expected to be equal
AlignmentPicker(alignment: self.$alignment)
}
}