Vincent Tourraine
Blog

Notes WWDC 2021 : What’s new in Foundation

#dev #iOS #macOS

Mes notes pour la session What’s new in Foundation de la WWDC 2021.

AttributedString

New struct AttributedString (value type, compatible with String, localizable, safety and security)

var thanks = AttributedString("Thank You")
thanks.font = .body.bold

Has “views”, as collection: characters, runs (aka attributes)

Localization: can now use "\(value)" in string literal for String and AttributedString. New compiler option “use compiler to extract Swift strings”.

Use Markdown to localize attributed strings.

Supports coding/decoding, only need custom implementation for custom attributes (need to conform to MarkdownDecodableAttributedStringKey).

Formatters

New type-safe interface for all formatters.

let date = Date.now
let f1 = date.formatted() // default
let f2 = date.formatted(.dateTime.year().day().month()) // “Jun 7, 2021”
let f3 = date.formatted(.dateTime.year().day().month(.wide)) // “June 7, 2021”
let f4 = date.formatted(.iso8601)

let range = (now..<later).formatted()

let attributed = date.formatted(.attributed) // as `AttributedString`
// can assign attributes by field

Fields: order does not matter, sensible default

Grammar agreement

For instance: adding “s” for plural in English

var body: some View {
  Text("Add ^[\(quantity) \(size) \(food)](inflect: true) to your order")
}

Currently supports Spanish and English only