<!--
{
  "availability" : [
    "DriverKit: -",
    "iOS: -",
    "iPadOS: -",
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "DriverKit",
  "identifier" : "/documentation/DriverKit/OSArray",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "DriverKit"
    ],
    "preciseIdentifier" : "c:@S@OSArray"
  },
  "title" : "OSArray"
}
-->

# OSArray

A container for an ordered, random-access collection of objects.

```
class OSArray;
```

## Overview

OSArray is a collection class for objects derived from OSObject. Storage and access are by array index.

You must generally cast retrieved objects from OSObject to the desired class using the OSDynamicCast macro. This macro returns the object cast to the desired class, or `NULL` if the object isn’t derived from that class.

As with all DriverKit collection classes, OSArray retains objects added to it, and releases objects removed from it (or replaced). An OSArray also grows as necessary to accommodate new objects.

OSArray provides no concurrency protection; it’s up to the usage context to provide any protection necessary.

## Topics

### Creating an Array

[`static OSArrayPtr withArray(const OSArray *array, uint32_t capacity);`](/documentation/DriverKit/OSArray/withArray)

Allocates an OSArray object with given members and preallocated capacity.

[`static OSArrayPtr withCapacity(uint32_t capacity);`](/documentation/DriverKit/OSArray/withCapacity)

Allocates an OSArray object with preallocated capacity.

[`static OSArrayPtr withObjects(const OSObject **values, uint32_t count, uint32_t capacity);`](/documentation/DriverKit/OSArray/withObjects)

Allocates an OSArray object with given members and preallocated capacity.

[`OSArrayPtrOSArrayCreate();`](/documentation/DriverKit/OSArrayCreate)

[`bool merge(const OSArray *otherArray);`](/documentation/DriverKit/OSArray/merge)

Appends all members of an array to this array.

[`virtual void free();`](/documentation/DriverKit/OSArray/free)

[`virtual void flushCollection();`](/documentation/DriverKit/OSArray/flushCollection)

Removes and drops references to all members of array.

### Accessing Elements

[`OSObject * getObject(uint32_t index) const;`](/documentation/DriverKit/OSArray/getObject)

Returns a member of the array.

[`OSObject * getLastObject() const;`](/documentation/DriverKit/OSArray/getLastObject)

Returns the last member of the array.

[`uint32_t getNextIndexOfObject(const OSMetaClassBase *anObject, uint32_t index) const;`](/documentation/DriverKit/OSArray/getNextIndexOfObject)

Searches the array for an object.

[`bool setObject(const OSMetaClassBase *anObject);`](/documentation/DriverKit/OSArray/setObject-3bore)

Appends an object as the last member of the array.

[`bool setObject(uint32_t index, const OSMetaClassBase *anObject);`](/documentation/DriverKit/OSArray/setObject-4ys3x)

Sets an object as the member of the array at a given index.

[`virtual bool iterateObjects(OSCollectionIterateObjectsBlock block) const;`](/documentation/DriverKit/OSArray/iterateObjects)

Iterates the array calling a callback block for each member.

[`bool replaceObject(uint32_t index, const OSMetaClassBase *anObject);`](/documentation/DriverKit/OSArray/replaceObject)

Removes a current member of the array and replaces it with another object.

[`void removeObject(uint32_t index);`](/documentation/DriverKit/OSArray/removeObject)

Removes a current member of the array.

[`bool OSArrayAppendValue(OSArrayPtr obj, OSObjectPtr value);`](/documentation/DriverKit/OSArrayAppendValue)

[`bool OSArrayReplaceValue(OSArrayPtr obj, size_t index, OSObjectPtr value);`](/documentation/DriverKit/OSArrayReplaceValue)

### Getting and Setting Values

[`OSObjectPtrOSArrayGetValue(OSArrayPtr obj, size_t index);`](/documentation/DriverKit/OSArrayGetValue)

[`const char *OSArrayGetStringValue(OSArrayPtr obj, size_t index);`](/documentation/DriverKit/OSArrayGetStringValue)

[`uint64_t OSArrayGetUInt64Value(OSArrayPtr obj, size_t index);`](/documentation/DriverKit/OSArrayGetUInt64Value)

[`bool OSArraySetValue(OSArrayPtr obj, size_t index, OSObjectPtr value);`](/documentation/DriverKit/OSArraySetValue)

[`void OSArraySetStringValue(OSArrayPtr obj, size_t index, const char *value);`](/documentation/DriverKit/OSArraySetStringValue)

[`void OSArraySetUInt64Value(OSArrayPtr obj, size_t index, uint64_t value);`](/documentation/DriverKit/OSArraySetUInt64Value)

[Array Indices](/documentation/DriverKit/3180989-array_indices)

[`typedef OSArray * OSArrayPtr;`](/documentation/DriverKit/OSArrayPtr)

### Inspecting an Array

[`virtual uint32_t getCount() const;`](/documentation/DriverKit/OSArray/getCount)

Returns count of members in array.

[`virtual uint32_t getCapacity() const;`](/documentation/DriverKit/OSArray/getCapacity)

Returns count of currently allocated capacity for members in array.

[`virtual uint32_t ensureCapacity(uint32_t newCapacity);`](/documentation/DriverKit/OSArray/ensureCapacity)

Allocates capacity for members in array.

[`uint32_t OSArrayGetCount(OSArrayPtr obj);`](/documentation/DriverKit/OSArrayGetCount)

### Modifying the Array Contents

[`bool OSArrayApply(OSArrayPtr obj, OSArrayApplier applier);`](/documentation/DriverKit/OSArrayApply)

[`typedef _Bool (^)(class OSObject *) OSArrayApplier;`](/documentation/DriverKit/OSArrayApplier)

### Comparing Arrays

[`bool isEqualTo(const OSArray *anArray) const;`](/documentation/DriverKit/OSArray/isEqualTo-5w7om)

Compares all members of two arrays with isEqualTo().

[`virtual bool isEqualTo(const OSMetaClassBase *anObject) const;`](/documentation/DriverKit/OSArray/isEqualTo-93qxy)

Compares the array with an OSObject

## Relationships

### Inherited By

[`OSSet`](/documentation/DriverKit/OSSet)

### Inherits From

[`OSCollection`](/documentation/DriverKit/OSCollection)

---

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)