<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSFastEnumeration/countByEnumerating(with:objects:count:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSFastEnumeration(im)countByEnumeratingWithState:objects:count:"
  },
  "title" : "countByEnumerating(with:objects:count:)"
}
-->

# countByEnumerating(with:objects:count:)

Returns by reference a C array of objects over which the sender should iterate, and as the return value the number of objects in the array.

```
func countByEnumerating(with state: UnsafeMutablePointer<NSFastEnumerationState>, objects buffer: AutoreleasingUnsafeMutablePointer<AnyObject?>, count len: Int) -> Int
```

## Parameters

`state`

Context information that is used in the enumeration to, in addition to other possibilities, ensure that the collection has not been mutated.

`buffer`

A C array of objects over which the sender is to iterate.

`len`

The maximum number of objects to return in `stackbuf`.

## Return Value

The number of objects returned in `stackbuf`. Returns `0` when the iteration is finished.

## Discussion

The state structure is assumed to be of stack local memory, so you can recast the passed in state structure to one more suitable for your iteration.

---

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)