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

# prefix(_:)

Republishes elements up to the specified maximum count.

```
func prefix(_ maxLength: Int) -> Publishers.Output<Self>
```

## Parameters

`maxLength`

The maximum number of elements to republish.

## Return Value

A publisher that publishes up to the specified number of elements.

## Discussion

Use [`prefix(_:)`](/documentation/Combine/Publisher/prefix(_:)) to limit the number of elements republished to the downstream subscriber.

In the example below, the [`prefix(_:)`](/documentation/Combine/Publisher/prefix(_:)) operator limits its output to the first two elements before finishing normally:

```
let numbers = (0...10)
cancellable = numbers.publisher
    .prefix(2)
    .sink { print("\($0)", terminator: " ") }

// Prints: "0 1"
```

---

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)