Contents

list(type:width:)

Returns a format style to format a list of strings.

Declaration

static func list<Base>(type: ListFormatStyle<StringStyle, Base>.ListType, width: ListFormatStyle<StringStyle, Base>.Width = .standard) -> Self where Self == ListFormatStyle<StringStyle, Base>, Base : Sequence, Base.Element == String

Parameters

  • type:

    The list type to apply, such as cumulative (And) or alternative (Or) elements.

  • width:

    The width to use when formatting, such as Standard or Narrow.

Return Value

A list format style that formats a string array as a textual list of items.

Discussion

Use the dot-notation form of this type method when the call point allows the use of ListFormatStyle, and the Sequence element type is String. You typically do this when calling the formatted(_:) method of Sequence.

The following example creates an array of strings, then uses formatted(_:) and the list format style provided by this method to format the items. It modifies the list format style to use the en_US locale, so the resulting string uses US English conventions for commas and conjunctions (“and”).

let items = ["Atlantic", "Pacific", "Indian", "Arctic", "Southern"]
let formatted = items.formatted(
    .list(type:.and)
    .locale(Locale(identifier: "en_US"))) // "Atlantic, Pacific, Indian, Arctic, and Southern"

See Also

Applying list styles