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

# OSDynamicCast

Casts an object safely to the specified type, if possible.

```
#define OSDynamicCast(type, inst)
```

## Parameters

`type`

The name of the desired class type as a raw token, not as a string or macro.

`inst`

The object to cast to the specified type.

## Return Value

The object cast to the specified type, or `NULL` if the cast could not be performed safely.

## Discussion

Use this macro instead of the standard C++ RTTI type-casting operator to cast objects to specific types. The following example shows how to cast some object to the [`OSString`](/documentation/DriverKit/OSString) class. If the object cannot be cast to a string, the macro sets the `string` variable to `NULL`.

```objc
OSString string = OSDynamicCast(OSString, anObject)
```

---

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)