<!--
{
  "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/Publisher/share()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE5shareAA10PublishersO5ShareCy_xGyF"
  },
  "title" : "share()"
}
-->

# share()

Shares the output of an upstream publisher with multiple subscribers.

```
func share() -> Publishers.Share<Self>
```

## Return Value

A class instance that shares elements received from its upstream to multiple subscribers.

## Discussion

The publisher returned by this operator supports multiple subscribers, all of whom receive unchanged elements and completion states from the upstream publisher.

> Tip: ``doc://com.apple.Combine/documentation/Combine/Publishers/Share`` is effectively a combination of the ``doc://com.apple.Combine/documentation/Combine/Publishers/Multicast`` and ``doc://com.apple.Combine/documentation/Combine/PassthroughSubject`` publishers, with an implicit ``doc://com.apple.Combine/documentation/Combine/ConnectablePublisher/autoconnect()``.

The following example uses a sequence publisher as a counter to publish three random numbers, generated by a [`map(_:)`](/documentation/Combine/Publisher/map(_:)-99evh) operator. It uses a [`share()`](/documentation/Combine/Publisher/share()) operator to share the same random number to each of two subscribers. This example uses a [`delay(for:tolerance:scheduler:options:)`](/documentation/Combine/Publisher/delay(for:tolerance:scheduler:options:)) operator only to prevent the first subscriber from exhausting the sequence publisher immediately; an asynchronous publisher wouldn’t need this.

```
let pub = (1...3).publisher
    .delay(for: 1, scheduler: DispatchQueue.main)
    .map( { _ in return Int.random(in: 0...100) } )
    .print("Random")
    .share()

cancellable1 = pub
    .sink { print ("Stream 1 received: \($0)")}
cancellable2 = pub
    .sink { print ("Stream 2 received: \($0)")}

// Prints:
// Random: receive value: (20)
// Stream 1 received: 20
// Stream 2 received: 20
// Random: receive value: (85)
// Stream 1 received: 85
// Stream 2 received: 85
// Random: receive value: (98)
// Stream 1 received: 98
// Stream 2 received: 98
```

Without the [`share()`](/documentation/Combine/Publisher/share()) operator, stream 1 receives three random values, followed by stream 2 receiving three different random values.

Also note that [`Publishers.Share`](/documentation/Combine/Publishers/Share) is a class rather than a structure like most other publishers. This means you can use this operator to create a publisher instance that uses reference semantics.

---

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)