Contents

objc_allocateClassPair(_:_:_:)

Creates a new class and metaclass.

Declaration

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

Parameters

  • superclass:

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

  • name:

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

  • extraBytes:

    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).

Discussion

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