What's New / App Services, Foundation & Diagnostics

What's new in Observation

+34 NewiOS · macOS · tvOS · watchOS · visionOS

Observation provides the tracking machinery behind the @Observable macro, watching property access and mutation on reference types so dependents can respond to changes. ObservationTracking is the entry point for registering and observing those accesses.

34 new APIs, no deprecations or removals. ObservationTracking gains an Options struct with init, willSet, didSet, and deinit, plus an Event struct carrying a kind and a matches method. Event.Kind adds the cases initial, willSet, didSet, and deinit, and there is a new Token struct.

New

34
extension

ObservationTracking

NewiOSmacOStvOSvisionOSwatchOS
extension ObservationTracking
Declaration
extension ObservationTracking {

    @available(anyAppleOS 27.0, *)
    public struct Token : ~Copyable {

        @available(anyAppleOS 27.0, *)
        public consuming func cancel()

        deinit
    }
}
extension

ObservationTracking.Options

NewiOSmacOStvOSvisionOSwatchOS
extension ObservationTracking.Options : SetAlgebra
Declaration
extension ObservationTracking.Options : SetAlgebra {

    /// Creates an instance initialized with the given elements.
    @available(anyAppleOS 27.0, *)
    public init(arrayLiteral elements: ObservationTracking.Options...)

    /// Returns a new set with the elements of both this and the given set.
    ///
    /// In the following example, the `attendeesAndVisitors` set is made up
    /// of the elements of the `attendees` and `visitors` sets:
    ///
    ///     let attendees: Set = ["Alicia", "Bethany", "Diana"]
    ///     let visitors = ["Marcia", "Nathaniel"]
    ///     let attendeesAndVisitors = attendees.union(visitors)
    ///     print(attendeesAndVisitors)
    ///     // Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"
    ///
    /// If the set already contains one or more elements that are also in
    /// `other`, the existing members are kept.
    ///
    ///     let initialIndices = Set(0..<5)
    ///     let expandedIndices = initialIndices.union([2, 3, 6, 7])
    ///     print(expandedIndices)
    ///     // Prints "[2, 4, 6, 7, 0, 1, 3]"
    ///
    /// - Parameter other: A set of the same type as the current set.
    /// - Returns: A new set with the unique elements of this set and `other`.
    ///
    /// - Note: if this set and `other` contain elements that are equal but
    ///   distinguishable (e.g. via `===`), which of these elements is present
    ///   in the result is unspecified.
    @available(anyAppleOS 27.0, *)
    public func union(_ other: ObservationTracking.Options) -> ObservationTracking.Options

    /// Returns a new set with the elements that are common to both this set and
    /// the given set.
    ///
    /// In the following example, the `bothNeighborsAndEmployees` set is made up
    /// of the elements that are in *both* the `employees` and `neighbors` sets.
    /// Elements that are in only one or the other are left out of the result of
    /// the intersection.
    ///
    ///     let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
    ///     let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
    ///     let bothNeighborsAndEmployees = employees.intersection(neighbors)
    ///     print(bothNeighborsAndEmployees)
    ///     // Prints "["Bethany", "Eric"]"
    ///
    /// - Parameter other: A set of the same type as the current set.
    /// - Returns: A new set.
    ///
    /// - Note: if this set and `other` contain elements that are equal but
    ///   distinguishable (e.g. via `===`), which of these elements is present
    ///   in the result is unspecified.
    @available(anyAppleOS 27.0, *)
    public func intersection(_ other: ObservationTracking.Options) -> ObservationTracking.Options

    /// Returns a new set with the elements that are either in this set or in the
    /// given set, but not in both.
    ///
    /// In the following example, the `eitherNeighborsOrEmployees` set is made up
    /// of the elements of the `employees` and `neighbors` sets that are not in
    /// both `employees` *and* `neighbors`. In particular, the names `"Bethany"`
    /// and `"Eric"` do not appear in `eitherNeighborsOrEmployees`.
    ///
    ///     let employees: Set = ["Alicia", "Bethany", "Diana", "Eric"]
    ///     let neighbors: Set = ["Bethany", "Eric", "Forlani"]
    ///     let eitherNeighborsOrEmployees = employees.symmetricDifference(neighbors)
    ///     print(eitherNeighborsOrEmployees)
    ///     // Prints "["Diana", "Forlani", "Alicia"]"
    ///
    /// - Parameter other: A set of the same type as the current set.
    /// - Returns: A new set.
    @available(anyAppleOS 27.0, *)
    public func symmetricDifference(_ other: ObservationTracking.Options) -> ObservationTracking.Options

    /// Adds the elements of the given set to the set.
    ///
    /// In the following example, the elements of the `visitors` set are added to
    /// the `attendees` set:

Truncated.

extension

ObservationTracking.Options

NewiOSmacOStvOSvisionOSwatchOS
extension ObservationTracking.Options : Sendable
Declaration
extension ObservationTracking.Options : Sendable {
}
func

withContinuousObservation

NewiOSmacOStvOSvisionOSwatchOS
public func withContinuousObservation(options: ObservationTracking.Options, apply: @escaping @isolated(any) @Sendable (borrowing ObservationTracking.Event) -> Void) -> ObservationTracking.Token
func

withObservationTracking

NewiOSmacOStvOSvisionOSwatchOS
public func withObservationTracking<Result, Failure>(options: ObservationTracking.Options, _ apply: () throws(Failure) -> Result, onChange: @escaping @Sendable (borrowing ObservationTracking.Event) -> Void) throws(Failure) -> Result where Failure : Error, Result : ~Copyable
struct

ObservationTracking.Event

NewiOSmacOStvOSvisionOSwatchOS
public struct Event : ~Copyable
Declaration
public struct Event : ~Copyable {

    @available(anyAppleOS 27.0, *)
    public struct Kind : Equatable, Sendable {

        @available(anyAppleOS 27.0, *)
        public static var initial: ObservationTracking.Event.Kind { get }

        @available(anyAppleOS 27.0, *)
        public static var willSet: ObservationTracking.Event.Kind { get }

        @available(anyAppleOS 27.0, *)
        public static var didSet: ObservationTracking.Event.Kind { get }

        @available(anyAppleOS 27.0, *)
        public static var `deinit`: ObservationTracking.Event.Kind { get }

        /// Returns a Boolean value indicating whether two values are equal.
        ///
        /// Equality is the inverse of inequality. For any values `a` and `b`,
        /// `a == b` implies that `a != b` is `false`.
        ///
        /// - Parameters:
        ///   - lhs: A value to compare.
        ///   - rhs: Another value to compare.
        public static func == (a: ObservationTracking.Event.Kind, b: ObservationTracking.Event.Kind) -> Bool
    }

    @available(anyAppleOS 27.0, *)
    public var kind: ObservationTracking.Event.Kind { get }

    @available(anyAppleOS 27.0, *)
    public func matches(_ keyPath: PartialKeyPath<some Observable>) -> Bool

    @available(anyAppleOS 27.0, *)
    public func cancel()
}
struct

ObservationTracking.Options

NewiOSmacOStvOSvisionOSwatchOS
public struct Options
Declaration
public struct Options {

    /// Creates an empty set.
    ///
    /// This initializer is equivalent to initializing with an empty array
    /// literal. For example, you create an empty `Set` instance with either
    /// this initializer or with an empty array literal.
    ///
    ///     var emptySet = Set<Int>()
    ///     print(emptySet.isEmpty)
    ///     // Prints "true"
    ///
    ///     emptySet = []
    ///     print(emptySet.isEmpty)
    ///     // Prints "true"
    @available(anyAppleOS 27.0, *)
    public init()

    @available(anyAppleOS 27.0, *)
    public static var willSet: ObservationTracking.Options { get }

    @available(anyAppleOS 27.0, *)
    public static var didSet: ObservationTracking.Options { get }

    @available(anyAppleOS 27.0, *)
    public static var `deinit`: ObservationTracking.Options { get }
}
struct

ObservationTracking.Token

NewiOSmacOStvOSvisionOSwatchOS
public struct Token : ~Copyable
Declaration
public struct Token : ~Copyable {

    @available(anyAppleOS 27.0, *)
    public consuming func cancel()

    deinit
}
func

ObservationTracking.Event.cancel

NewiOSmacOStvOSvisionOSwatchOS
public func cancel()
var

ObservationTracking.Event.kind

NewiOSmacOStvOSvisionOSwatchOS
public var kind: ObservationTracking.Event.Kind { get }
struct

ObservationTracking.Event.Kind

NewiOSmacOStvOSvisionOSwatchOS
public struct Kind : Equatable, Sendable
Declaration
public struct Kind : Equatable, Sendable {

    @available(anyAppleOS 27.0, *)
    public static var initial: ObservationTracking.Event.Kind { get }

    @available(anyAppleOS 27.0, *)
    public static var willSet: ObservationTracking.Event.Kind { get }

    @available(anyAppleOS 27.0, *)
    public static var didSet: ObservationTracking.Event.Kind { get }

    @available(anyAppleOS 27.0, *)
    public static var `deinit`: ObservationTracking.Event.Kind { get }

    /// Returns a Boolean value indicating whether two values are equal.
    ///
    /// Equality is the inverse of inequality. For any values `a` and `b`,
    /// `a == b` implies that `a != b` is `false`.
    ///
    /// - Parameters:
    ///   - lhs: A value to compare.
    ///   - rhs: Another value to compare.
    public static func == (a: ObservationTracking.Event.Kind, b: ObservationTracking.Event.Kind) -> Bool
}
func

ObservationTracking.Event.matches

NewiOSmacOStvOSvisionOSwatchOS
public func matches(_ keyPath: PartialKeyPath<some Observable>) -> Bool
var

ObservationTracking.Event.Kind.deinit

NewiOSmacOStvOSvisionOSwatchOS
public static var `deinit`: ObservationTracking.Event.Kind { get }
var

ObservationTracking.Event.Kind.didSet

NewiOSmacOStvOSvisionOSwatchOS
public static var didSet: ObservationTracking.Event.Kind { get }
var

ObservationTracking.Event.Kind.initial

NewiOSmacOStvOSvisionOSwatchOS
public static var initial: ObservationTracking.Event.Kind { get }
var

ObservationTracking.Event.Kind.willSet

NewiOSmacOStvOSvisionOSwatchOS
public static var willSet: ObservationTracking.Event.Kind { get }
typealias

ObservationTracking.Options.ArrayLiteralElement

NewiOSmacOStvOSvisionOSwatchOS
public typealias ArrayLiteralElement = ObservationTracking.Options

The type of the elements of an array literal.

func

ObservationTracking.Options.contains

NewiOSmacOStvOSvisionOSwatchOS
public func contains(_ member: ObservationTracking.Options) -> Bool

Returns a Boolean value that indicates whether the given element exists in the set.

This example uses the contains(_:) method to test whether an integer is a member of a set of prime numbers.

let primes: Set = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
let x = 5
if primes.contains(x) {
    print("\(x) is prime!")
} else {
    print("\(x). Not prime.")
}
// Prints "5 is prime!"

Parameters

member
An element to look for in the set.

Returnstrue if member exists in the set; otherwise, false.

var

ObservationTracking.Options.deinit

NewiOSmacOStvOSvisionOSwatchOS
public static var `deinit`: ObservationTracking.Options { get }
var

ObservationTracking.Options.didSet

NewiOSmacOStvOSvisionOSwatchOS
public static var didSet: ObservationTracking.Options { get }
typealias

ObservationTracking.Options.Element

NewiOSmacOStvOSvisionOSwatchOS
public typealias Element = ObservationTracking.Options

A type for which the conforming type provides a containment test.

func

ObservationTracking.Options.formIntersection

NewiOSmacOStvOSvisionOSwatchOS
public mutating func formIntersection(_ other: ObservationTracking.Options)

Removes the elements of this set that aren't also in the given set.

In the following example, the elements of the employees set that are not also members of the neighbors set are removed. In particular, the names "Alicia", "Chris", and "Diana" are removed.

var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
employees.formIntersection(neighbors)
print(employees)
// Prints "["Bethany", "Eric"]"

Parameters

other
A set of the same type as the current set.
func

ObservationTracking.Options.formSymmetricDifference

NewiOSmacOStvOSvisionOSwatchOS
public mutating func formSymmetricDifference(_ other: ObservationTracking.Options)

Removes the elements of the set that are also in the given set and adds the members of the given set that are not already in the set.

In the following example, the elements of the employees set that are also members of neighbors are removed from employees, while the elements of neighbors that are not members of employees are added to employees. In particular, the names "Bethany" and "Eric" are removed from employees while the name "Forlani" is added.

var employees: Set = ["Alicia", "Bethany", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani"]
employees.formSymmetricDifference(neighbors)
print(employees)
// Prints "["Diana", "Forlani", "Alicia"]"

Parameters

other
A set of the same type.
func

ObservationTracking.Options.formUnion

NewiOSmacOStvOSvisionOSwatchOS
public mutating func formUnion(_ other: ObservationTracking.Options)

Adds the elements of the given set to the set.

In the following example, the elements of the visitors set are added to the attendees set:

var attendees: Set = ["Alicia", "Bethany", "Diana"]
let visitors: Set = ["Diana", "Marcia", "Nathaniel"]
attendees.formUnion(visitors)
print(attendees)
// Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"

If the set already contains one or more elements that are also in other, the existing members are kept.

var initialIndices = Set(0..<5)
initialIndices.formUnion([2, 3, 6, 7])
print(initialIndices)
// Prints "[2, 4, 6, 7, 0, 1, 3]"

Parameters

other
A set of the same type as the current set.
init

ObservationTracking.Options.init

NewiOSmacOStvOSvisionOSwatchOS
public init()

Creates an empty set.

This initializer is equivalent to initializing with an empty array literal. For example, you create an empty Set instance with either this initializer or with an empty array literal.

var emptySet = Set<Int>()
print(emptySet.isEmpty)
// Prints "true"

emptySet = []
print(emptySet.isEmpty)
// Prints "true"
init

ObservationTracking.Options.init

NewiOSmacOStvOSvisionOSwatchOS
public init(arrayLiteral elements: ObservationTracking.Options...)

Creates an instance initialized with the given elements.

func

ObservationTracking.Options.insert

NewiOSmacOStvOSvisionOSwatchOS
public mutating func insert(_ newMember: ObservationTracking.Options) -> (inserted: Bool, memberAfterInsert: ObservationTracking.Options)

Inserts the given element in the set if it is not already present.

If an element equal to newMember is already contained in the set, this method has no effect. In this example, a new element is inserted into classDays, a set of days of the week. When an existing element is inserted, the classDays set does not change.

enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday
}

var classDays: Set<DayOfTheWeek> = [.wednesday, .friday]
print(classDays.insert(.monday))
// Prints "(true, .monday)"
print(classDays)
// Prints "[.friday, .wednesday, .monday]"

print(classDays.insert(.friday))
// Prints "(false, .friday)"
print(classDays)
// Prints "[.friday, .wednesday, .monday]"

Parameters

newMember
An element to insert into the set.

Returns(true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means.

func

ObservationTracking.Options.intersection

NewiOSmacOStvOSvisionOSwatchOS
public func intersection(_ other: ObservationTracking.Options) -> ObservationTracking.Options

Returns a new set with the elements that are common to both this set and the given set.

In the following example, the bothNeighborsAndEmployees set is made up of the elements that are in *both* the employees and neighbors sets. Elements that are in only one or the other are left out of the result of the intersection.

let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
let bothNeighborsAndEmployees = employees.intersection(neighbors)
print(bothNeighborsAndEmployees)
// Prints "["Bethany", "Eric"]"

Note: if this set and other contain elements that are equal but distinguishable (e.g. via ===), which of these elements is present in the result is unspecified.

Parameters

other
A set of the same type as the current set.

ReturnsA new set.

func

ObservationTracking.Options.remove

NewiOSmacOStvOSvisionOSwatchOS
public mutating func remove(_ member: ObservationTracking.Options) -> ObservationTracking.Options?

Removes the given element and any elements subsumed by the given element.

Parameters

member
The element of the set to remove.

ReturnsFor ordinary sets, an element equal to member if member is contained in the set; otherwise, nil. In some cases, a returned element may be distinguishable from member by identity comparison or some other means. For sets where the set type and element type are the same, like OptionSet types, this method returns any intersection between the set and [member], or nil if the intersection is empty.

func

ObservationTracking.Options.symmetricDifference

NewiOSmacOStvOSvisionOSwatchOS
public func symmetricDifference(_ other: ObservationTracking.Options) -> ObservationTracking.Options

Returns a new set with the elements that are either in this set or in the given set, but not in both.

In the following example, the eitherNeighborsOrEmployees set is made up of the elements of the employees and neighbors sets that are not in both employees *and* neighbors. In particular, the names "Bethany" and "Eric" do not appear in eitherNeighborsOrEmployees.

let employees: Set = ["Alicia", "Bethany", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani"]
let eitherNeighborsOrEmployees = employees.symmetricDifference(neighbors)
print(eitherNeighborsOrEmployees)
// Prints "["Diana", "Forlani", "Alicia"]"

Parameters

other
A set of the same type as the current set.

ReturnsA new set.

func

ObservationTracking.Options.union

NewiOSmacOStvOSvisionOSwatchOS
public func union(_ other: ObservationTracking.Options) -> ObservationTracking.Options

Returns a new set with the elements of both this and the given set.

In the following example, the attendeesAndVisitors set is made up of the elements of the attendees and visitors sets:

let attendees: Set = ["Alicia", "Bethany", "Diana"]
let visitors = ["Marcia", "Nathaniel"]
let attendeesAndVisitors = attendees.union(visitors)
print(attendeesAndVisitors)
// Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"

If the set already contains one or more elements that are also in other, the existing members are kept.

let initialIndices = Set(0..<5)
let expandedIndices = initialIndices.union([2, 3, 6, 7])
print(expandedIndices)
// Prints "[2, 4, 6, 7, 0, 1, 3]"

Note: if this set and other contain elements that are equal but distinguishable (e.g. via ===), which of these elements is present in the result is unspecified.

Parameters

other
A set of the same type as the current set.

ReturnsA new set with the unique elements of this set and other.

func

ObservationTracking.Options.update

NewiOSmacOStvOSvisionOSwatchOS
public mutating func update(with newMember: ObservationTracking.Options) -> ObservationTracking.Options?

Inserts the given element into the set unconditionally.

If an element equal to newMember is already contained in the set, newMember replaces the existing element. In this example, an existing element is inserted into classDays, a set of days of the week.

enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday
}

var classDays: Set<DayOfTheWeek> = [.monday, .wednesday, .friday]
print(classDays.update(with: .monday))
// Prints "Optional(.monday)"

Parameters

newMember
An element to insert into the set.

ReturnsFor ordinary sets, an element equal to newMember if the set already contained such a member; otherwise, nil. In some cases, the returned element may be distinguishable from newMember by identity comparison or some other means. For sets where the set type and element type are the same, like OptionSet types, this method returns any intersection between the set and [newMember], or nil if the intersection is empty.

var

ObservationTracking.Options.willSet

NewiOSmacOStvOSvisionOSwatchOS
public static var willSet: ObservationTracking.Options { get }
func

ObservationTracking.Token.cancel

NewiOSmacOStvOSvisionOSwatchOS
public consuming func cancel()

No APIs match your filter.

← More in App Services, Foundation & Diagnostics