Skip to content

Type MethodFoundationEssentials

list(memberStyle:type:width:)

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
Returns a format style to format a list of items.
static func list<MemberStyle, Base>(memberStyle: MemberStyle, type: ListFormatStyle<MemberStyle, Base>.ListType, width: ListFormatStyle<MemberStyle, Base>.Width = .standard) -> Self where Self == ListFormatStyle<MemberStyle, Base>, MemberStyle : FormatStyle, Base : Sequence, MemberStyle.FormatInput == Base.Element, MemberStyle.FormatOutput == String

Parameters

memberStyle

The format style to apply to each item in the list.

type

The list type to apply, such as cumulative (ListFormatStyle.ListType.and) or alternative (ListFormatStyle.ListType.or) elements.

width

The width to use when formatting, such as ListFormatStyle.Width.standard or ListFormatStyle.Width.narrow.

Return Value

A list format style that formats an 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. You typically do this when calling the formatted method of Sequence.

The following example creates an array of integers, then uses the list format style provided by this method to format the items. By using a currency IntegerFormatStyle, the list format style expresses each member as US dollars.

let items: [Int] = [100, 1000, 10000, 100000, 1000000]
let formatted = items.formatted(
    .list(memberStyle: .currency(code: "USD"),
          type: .and)
    .locale(Locale(identifier: "en_US"))) // "$100.00, $1,000.00, $10,000.00, $100,000.00, and $1,000,000.00"