<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.5.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "ObjectiveC",
  "identifier" : "/documentation/ObjectiveC/class_addIvar(_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Objective-C Runtime"
    ],
    "preciseIdentifier" : "c:@F@class_addIvar"
  },
  "title" : "class_addIvar(_:_:_:_:_:)"
}
-->

# class_addIvar(_:_:_:_:_:)

Adds a new instance variable to a class.

```
func class_addIvar(_ cls: AnyClass?, _ name: UnsafePointer<CChar>, _ size: Int, _ alignment: UInt8, _ types: UnsafePointer<CChar>?) -> Bool
```

## Return Value

[`YES`](/documentation/ObjectiveC/YES) if the instance variable was added successfully, otherwise [`NO`](/documentation/ObjectiveC/NO) (for example, the class already contains an instance variable with that name).

## Discussion

This function may only be called after [`objc_allocateClassPair(_:_:_:)`](/documentation/ObjectiveC/objc_allocateClassPair(_:_:_:)) and before [`objc_registerClassPair(_:)`](/documentation/ObjectiveC/objc_registerClassPair(_:)). Adding an instance variable to an existing class is not supported.

The class must not be a metaclass. Adding an instance variable to a metaclass is not supported.

The instance variable’s minimum alignment in bytes is `1<<align`. The minimum alignment of an instance variable depends on the ivar’s type and the machine architecture. For variables of any pointer type, pass `log2(sizeof(pointer_type))`.

---

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)