---
title: OSCollectionIterator
framework: kernel
role: symbol
role_heading: Class
path: kernel/oscollectioniterator
---

# OSCollectionIterator

## Declaration

```occ
class OSCollectionIterator : OSIterator
```

## Overview

Overview OSCollectionIterator defines a consistent mechanism to iterate through the objects of an OSCollection. It expands on the basic interface of OSIterator to allow association of an iterator with a specific collection. To use an OSCollectionIterator, you create it with the collection to be iterated, then call OSIterator as long as it returns an object: <pre> OSCollectionIterator * iterator = OSCollectionIterator::withCollection(myCollection); OSObject * object; while (object = iterator->getNextObject()) { // do something with object } // optional if (!iterator->isValid()) { // report that collection changed during iteration } iterator->release(); </pre> Note that when iterating associative collections, the objects returned by getNextObject are keys; if you want to work with the associated values, simply look them up in the collection with the keys. Use Restrictions With very few exceptions in the I/O Kit, all Libkern-based C++ classes, functions, and macros are unsafe to use in a primary interrupt context. Consult the I/O Kit documentation related to primary interrupts for more information. OSCollectionIterator provides no concurrency protection.

## Topics

### Miscellaneous

- [free](kernel/oscollectioniterator/1808113-free.md)
- [getNextObject](kernel/oscollectioniterator/1808125-getnextobject.md)
- [initWithCollection](kernel/oscollectioniterator/1808147-initwithcollection.md)
- [isValid](kernel/oscollectioniterator/1808157-isvalid.md)
- [reset](kernel/oscollectioniterator/1808170-reset.md)
- [withCollection](kernel/oscollectioniterator/1808183-withcollection.md)

### Instance Methods

- [free](kernel/oscollectioniterator/1542649-free.md)
- [getMetaClass](kernel/oscollectioniterator/1542654-getmetaclass.md)
- [getNextObject](kernel/oscollectioniterator/1542659-getnextobject.md)
- [initWithCollection](kernel/oscollectioniterator/1542661-initwithcollection.md)
- [isValid](kernel/oscollectioniterator/1542655-isvalid.md)
- [reset](kernel/oscollectioniterator/1542652-reset.md)

### Type Methods

- [withCollection](kernel/oscollectioniterator/1542657-withcollection.md)

## Relationships

### Inherits From

- [OSIterator](kernel/ositerator.md)

## See Also

### Collections

- [OSArray](kernel/osarray.md)
- [OSDictionary](kernel/osdictionary.md)
- [OSSet](kernel/osset.md)
- [OSOrderedSet](kernel/osorderedset.md)
- [OSCollection](kernel/oscollection.md)
- [OSIterator](kernel/ositerator.md)
