Foundation updates
Learn about important changes to Foundation.
Overview
Browse notable changes in Foundation.
June 2025
Post and observe concurrency-safe notifications with new features in NotificationCenter that support Swift-friendly “message” types. Use NotificationCenter.MainActorMessage for notification messages bound to the MainActor and NotificationCenter.AsyncMessage for messages that are Sendable. Add an observer to one of these notification message types with the various
addObserver(of:for:using:)methods in NotificationCenter. Many standard notifications in Foundation, UIKit, and AppKit now offer a message-based API, associating the message with an existing Notification.Name. You can also associate a message with a new name for Swift-only notifications.Simplify usage of UndoManager on the main actor, now that the undo manager is marked with
@MainActor. The undo manager also adds a setActionName(_:) method that takes a LocalizedStringResource. Use this with the App Intents framework, which introduces a new UndoableIntent.Access your app, app extension, or framework’s bundle with the
#bundlemacro. Using the macro is more performant and convenient than using.selfor a bundle identifier, particularly when loading localized strings with initializers that take abundle:parameter. The macro back-deploys, so you can use it with projects whose deployment targets specify earlier versions of the operating system.Get UTF-8 and UTF-16 views of an AttributedString with the utf8 and utf16 methods of AttributedStringProtocol.
Access multiple ranges of an AttributedString with the new DiscontiguousAttributedSubstring type. You can get this type from an attributed string with either a RangeSet of indices, or with the new AttributedTextSelection type.
June 2024
Predicates
Use
Regexregular expressions in predicates to perform pattern matching. You can use either the Swift regex domain specific language or traditional regex literals.Use the new
#Expressionmacro when you want behavior similar to#Predicatebut you need to return a type other thanBool.Create compound predicates by writing a
Predicatethat evaluates anotherPredicate.
Calendars
Perform
Calendarsearches by callingdates(byMatching:startingAt:in:matchingPolicy:repeatedTimePolicy:direction:)withDateComponentvalues specifying what to search for, and receiving aSequenceof matching dates. You can also repeatedly addDateComponentvalues to a date withdates(byAdding:startingAt:in:wrappingComponents:)and receive a sequence. To addCalendar.Componentvalues instead, usedates(byAdding:value:startingAt:in:wrappingComponents:).
Format styles
Use the
DiscreteFormatStyleprotocol to format values that constantly change, but don’t necessarily change the formatted output on every update, such as time displays that truncate the seconds field. The following format style types conform to the new protocol:Duration.UnitsFormatStyle,Duration.TimeFormatStyle,Date.FormatStyle,Date.FormatStyle.Attributed,Date.VerbatimFormatStyle,Date.VerbatimFormatStyle.Attributed,Date.ISO8601FormatStyle,Duration.UnitsFormatStyle.Attributed, andDuration.TimeFormatStyle.Attributed.Set new configuration options on
FormatStyleto omit specific symbols from the output and suppress grouping of large time values (for example, in order to produce10000:00rather than10,000:00).Use the
notationmodifier on currency format styles to specify a notation such ascompactName, similar to hownotationalready works on numeric format styles.
Undo manager
Provide custom information for undo actions by setting user info on
UndoManager. The info can include things like a timestamp of the action’s creation or an icon that complements the action name.Reveal the size of an undo manager’s current undo or redo stack with the properties
undoCountandredoCount.
Key-Value observing
Add a collection of shared observers to
Observableobjects that you can then add to multiple instance of the observable.Observables now use ARC-style weak references to their observers, preventing crashes when observers deallocate without properly removing themselves.
Collections
You can initialize an
IndexSetfrom a SwiftRangeSet, and vice versa.
See Also
Technology and frameworks
Accelerate updatesAccessibility updatesActivityKit updatesAdAttributionKit UpdatesApp Clips updatesApp Intents updatesAppKit updatesApple Intelligence updatesAppleMapsServerAPI UpdatesApple Pencil updatesARKit updatesAudio Toolbox updatesAuthenticationServices updatesAVFAudio updatesAVFoundation updates