Type MethodFoundationEssentials
list(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 strings.
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
typeThe list type to apply, such as cumulative (
ListFormatStyle.ListType.and) or alternative (ListFormatStyle.ListType.or) elements.widthThe width to use when formatting, such as
ListFormatStyle.Width.standardorListFormatStyle.Width.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 the list format style provided by this method to format the items.
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"