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

# collect(_:)

Collects up to the specified number of elements, and then emits a single array of the collection.

```
func collect(_ count: Int) -> Publishers.CollectByCount<Self>
```

## Parameters

`count`

The maximum number of received elements to buffer before publishing.

## Return Value

A publisher that collects up to the specified number of elements, and then publishes them as an array.

## Discussion

Use [`collect(_:)`](/documentation/Combine/Publisher/collect(_:)) to emit arrays of at most `count` elements from an upstream publisher. If the upstream publisher finishes before collecting the specified number of elements, the publisher sends an array of only the items it received. This may be fewer than `count` elements.

If the upstream publisher fails with an error, this publisher forwards the error to the downstream receiver instead of sending its output.

In the example below, the [`collect(_:)`](/documentation/Combine/Publisher/collect(_:)) operator emits one partial and two full arrays based on the requested collection size of `5`:

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

// Prints "[0, 1, 2, 3, 4] [5, 6, 7, 8, 9] [10] "
```

> Note: When this publisher receives a request for `.max(n)` elements, it requests `.max(count * n)` from the upstream publisher.

---

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)