init(_:sources:isOn:)
Creates a toggle representing a collection of values that generates its label from a localized string key.
Declaration
nonisolated init<C>(_ titleKey: LocalizedStringKey, sources: C, isOn: KeyPath<C.Element, Binding<Bool>>) where C : RandomAccessCollectionParameters
- titleKey:
The key for the toggle’s localized title, that describes the purpose of the toggle.
- sources:
A collection of values used as the source for rendering the Toggle’s state.
- isOn:
The key path of the values that determines whether the toggle is on, mixed or off.
Discussion
This initializer creates a Text view on your behalf, and treats the localized key similar to init(_:tableName:bundle:comment:). See Text for more information about localizing strings.
The following example creates a single toggle that represents the state of multiple alarms:
struct Alarm: Hashable, Identifiable {
var id = UUID()
var isOn = false
var name = ""
}
@State private var alarms = [
Alarm(isOn: true, name: "Morning"),
Alarm(isOn: false, name: "Evening")
]
Toggle("Enable all alarms", sources: $alarms, isOn: \.isOn)