Skip to content

Instance Method

choose(_:from:)

Prompts the user to choose an item from the supplied array. The chosen item will be returned.
func choose<T>(_ prompt: ConsoleText, from array: [T]) -> T where T : CustomStringConvertible

Parameters

prompt

ConsoleText prompt to display to the user before listing options.

array

Array of CustomStringConvertible items to choose from.

Return Value

Element from array that the user chose.

Discussion

let color = console.choose("Favorite color?", from: ["Pink", "Blue"])
console.output("You chose: " + color.consoleText())

The above code will output:

Favorite color?
1: Pink
2: Blue
>

Upon answering, the prompt and options will be cleared from the console and only the output will remain:

You chose: Blue

This method calls choose(_:from:display:) using CustomStringConvertible to display each element.