<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publishers/SwitchToLatest",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine10PublishersO14SwitchToLatestV"
  },
  "title" : "Publishers.SwitchToLatest"
}
-->

# Publishers.SwitchToLatest

A publisher that flattens nested publishers.

```
struct SwitchToLatest<P, Upstream> where P : Publisher, P == Upstream.Output, Upstream : Publisher, P.Failure == Upstream.Failure
```

## Overview

Given a publisher that publishes [`Publisher`](/documentation/Combine/Publisher) instances, the [`Publishers.SwitchToLatest`](/documentation/Combine/Publishers/SwitchToLatest) publisher produces a sequence of events from only the most recent one.
For example, given the type `AnyPublisher<URLSession.DataTaskPublisher,NSError>`, calling `switchToLatest()` results in the type `SwitchToLatest<(Data, URLResponse), URLError>`.
The downstream subscriber sees a continuous stream of `(Data, URLResponse)` elements from what looks like a single <doc://com.apple.documentation/documentation/Foundation/URLSession/DataTaskPublisher> even though the elements are coming from different upstream publishers.

When [`Publishers.SwitchToLatest`](/documentation/Combine/Publishers/SwitchToLatest) receives a new publisher from the upstream publisher, it cancels its previous subscription.
Use this feature to prevent earlier publishers from performing unnecessary work, such as creating network request publishers from frequently-updating user interface publishers.

## Topics

### Creating a switch-to-latest Publisher

[`init(upstream:)`](/documentation/Combine/Publishers/SwitchToLatest/init(upstream:))

Creates a publisher that “flattens” nested publishers.

### Declaring supporting types

[`Publishers.SwitchToLatest.Output`](/documentation/Combine/Publishers/SwitchToLatest/Output)

The kind of values published by this publisher.

[`Publishers.SwitchToLatest.Failure`](/documentation/Combine/Publishers/SwitchToLatest/Failure)

The kind of errors this publisher might publish.

### Inspecting publisher properties

[`upstream`](/documentation/Combine/Publishers/SwitchToLatest/upstream)

The publisher from which this publisher receives elements.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)