AppEnum
An interface to express that a custom type has a predefined, static set of values.
Declaration
protocol AppEnum : AppValue, StaticDisplayRepresentable, RawRepresentable where Self.RawValue : LosslessStringConvertibleMentioned in
Overview
When you want an app intent parameter or app entity property to have a fixed set of values, set the type of the underlying property to one that adopts the AppEnum protocol. In custom code, you use enumerations to limit the number of values available to a property of that type. For example, a fitness app might specify the available workout types using an enumeration instead of a string, because the enumeration requires someone to choose only known values. The AppEnum protocol adds metadata that Siri and other system features require to interact with your enumeration or custom type.
Add the AppEnum protocol to an existing enumeration or type you plan to use in an app intent or app entity. For best results, base your enumeration on a type that’s easily convertible to a string such as the String or Int type.
The AppEnum protocol adds conformance to several other protocols to your type, including the TypeDisplayRepresentable and CaseDisplayRepresentable protocols. You’re responsible for implementing these protocols and providing descriptions of your type and each of the cases it contains. The following example shows an enumeration that a workout app uses to specify the available activities. The implementations of the typeDisplayRepresentation and caseDisplayRepresentations properties provide the protocol-mandated descriptions of the type and each activity. The system uses your descriptions in dialogs and to resolve parameters more quickly.
enum ActivityStyle: String, AppEnum {
case biking
case equestrian
case hiking
case jogging
case crossCountrySkiing
case snowshoeing
// Describe the overall type.
static var typeDisplayRepresentation: TypeDisplayRepresentation {
TypeDisplayRepresentation(
name: LocalizedStringResource("Activity style", table: "AppIntents"),
numericFormat: LocalizedStringResource("\(placeholder: .int) data", table: "AppIntents"))
}
// Describe the individual cases.
static var caseDisplayRepresentations: [Self: DisplayRepresentation] = [
.biking: DisplayRepresentation(title: "Biking", subtitle: "Mountain bike ride"),
.equestrian: DisplayRepresentation(title: "Equestrian", subtitle: "Equestrian sports"),
.hiking: DisplayRepresentation(title: "Hiking", subtitle: "A lengthy outdoor walk"),
.jogging: DisplayRepresentation(title: "Jogging", subtitle: "A gentle run"),
.crossCountrySkiing: DisplayRepresentation(title: "Skiing", subtitle: "Cross-country skiing"),
.snowshoeing: DisplayRepresentation(title: "Snowshoeing", subtitle: "Walking in the snow")
]
}