Creates a new class and metaclass.


func objc_allocateClassPair(_ superclass: AnyClass?, _ name: UnsafePointer<Int8>, _ extraBytes: Int) -> AnyClass?



The class to use as the new class's superclass, or Nil to create a new root class.


The string to use as the new class's name. The string will be copied.


The number of bytes to allocate for indexed ivars at the end of the class and metaclass objects. This should usually be 0.

Return Value

The new class, or Nil if the class could not be created (for example, the desired name is already in use).


You can get a pointer to the new metaclass by calling object_getClass(newClass).

To create a new class, start by calling objc_allocateClassPair(_:_:_:). Then set the class's attributes with functions like class_addMethod(_:_:_:_:) and class_addIvar(_:_:_:_:_:). When you are done building the class, call objc_registerClassPair(_:). The new class is now ready for use.

Instance methods and instance variables should be added to the class itself. Class methods should be added to the metaclass.

See Also

Adding Classes

func objc_disposeClassPair(AnyClass)

Destroys a class and its associated metaclass.

func objc_registerClassPair(AnyClass)

Registers a class that was allocated using objc_allocateClassPair(_:_:_:).