WWDC 2021: What's new in Foundation

Find hereafter a detailed summary of the above named video which belongs to a taxonomy of some WWDC footages.

The original video is available on the official Apple website (session 10109).

"Discover how the latest updates to Foundation can help you improve your app's localization and internationalization support. Find out about the new AttributedString, designed specifically for Swift, and learn how you can use Markdown to apply style to your localized strings. Explore the grammar agreement engine, which automatically fixes up localized strings so they match grammatical gender and pluralization. And we'll take you through improvements to date and number formatting that simplify complex requirements while also improving performance."


Various contents of this video are indicated hereunder:


Most of the illustrations are parts of the Apple presentations and may be available at the Resources section inside the Overview sheet of each video.

Hereafter, the underlined elements lead directly to the playback of the WWDC video at the appropriate moment.


AttributedString #

🎬 (01:06)

The initial reference type NSAttributedString now turns into a brand new struct AttributedString that is henceforth a value type with two new views: characters and runs.

The character view that provides access to the string uses indices to reach this goal.
The run view is the starting location, length and value of a particular attribute.

Some examples are provided to take full advantage of the new AttributedString features: the first one deals with the basics...

... while the second one highlights the use of indices by the character view.

Likewise, examples are detailed to understand the AttributedString runs counting...

... and the AttributedString mutation.

🎬 (07:02)

The string interpolation (mix of constants, variables, literals, and expressions by including their values inside a string literal) is used to support localized formatting of AttributedString.

The Xcode compiler automatically generates the strings files when specified in the Build Settings section.

Localized attributed strings can now use Markdown to get their attributes.

🎬 (08:45)

A default Codable protocol conformance is the only thing to do in order to use the AttributedString basic encoding and decoding.

When using a custom attribute, the AttributedStringKey protocol makes the magic by requiring the type of value and the name for archiving.

To make it completely Codable, the example highlights the protocols to be opted in: CodableAttributedStringKey and MarkdownDecodableAttributedStringKey.

Many examples are detailed to harness the custom attribute syntax for Markdown not to mention the introduction of the attribute scope concept.

Scopes are a group of attribute keys that is extremely useful when decoding from JSON or Markdown.
The provided example shows up the way to define a scope of attributes while using the keypath syntax in its calling.



Formatters #

🎬 (14:35)

There's a brand new Formatter API whose performance and usability have been improved and devised from the ground up.
The way to convert the data the formatter is responsible into a localized and user-presentable string follows a new rationale.

This new technique including autocompletion and type safety has been applied to all ten formatters in Foundation.

🎬 (17:03)

Now, very few lines of code are needed to format a date.

The new formatting follows this pattern: the value to be formatted is the first element, next is the formatted function and, finally, the argument that represents the style.

There's also a new way to deal with dates intervals.

A great new feature in formatting is attributed output.

🎬 (23:49)

The goal is to parse a Date element and be sure that it's formatted as desired thanks to a strategy parameter that is used to tell the parser what fields to expect in the input.

A fixed format may also be used in conjunction with string interpolaton.

🎬 (25:13)

Dealing with Number formatting aims at getting the correct output for every user’s locale as the examples show up.



Grammar agreement #

🎬 (27:06)

Agreement is the adaptation of the gender and the pluralization to the different use cases the application may suggest.

According to the languages, it may become convoluted to deal with all the possible combinations.
The solution for this problem is the Automatic Grammar Agreement associated with the inflection process that will handle these topics relying on some appropriate input parameters.

This new API effectively handles some situations in which many lines of code were required to get the same result.

Besides the gender and the pluralization, it's now possible to take into account the user’s preferred term of address in apps.

If there's no information about this user’s preference, an inflection alternative can be appended.

A complete demo is also available to notice how it looks like and it works in an app.

In the complete demo, export, modify and import the localizations are highlighted using the Xcode compiler which makes the job easier.