<!--
{
  "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_addMethod(_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Objective-C Runtime"
    ],
    "preciseIdentifier" : "c:@F@class_addMethod"
  },
  "title" : "class_addMethod(_:_:_:_:)"
}
-->

# class_addMethod(_:_:_:_:)

Adds a new method to a class with a given name and implementation.

```
func class_addMethod(_ cls: AnyClass?, _ name: Selector, _ imp: IMP, _ types: UnsafePointer<CChar>?) -> Bool
```

## Parameters

`cls`

The class to which to add a method.

`name`

A selector that specifies the name of the method being added.

`imp`

A function which is the implementation of the new method. The function must take at least two arguments—`self` and `_cmd`.

`types`

An array of characters that describe the types of the arguments to the method. For possible values, see [Objective-C Runtime Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048) > [Type Encodings](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100). Since the function must take at least two arguments—`self` and `_cmd`, the second and third characters must be “`@:`” (the first character is the return type).

## Return Value

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

## Discussion

[`class_addMethod(_:_:_:_:)`](/documentation/ObjectiveC/class_addMethod(_:_:_:_:)) will add an override of a superclass’s implementation, but will not replace an existing implementation in this class. To change an existing implementation, use [`method_setImplementation(_:_:)`](/documentation/ObjectiveC/method_setImplementation(_:_:)).

An Objective-C method is simply a C function that take at least two arguments—`self` and `_cmd`. For example, given the following function:

```objc
void myMethodIMP(id self, SEL _cmd)
{
    // implementation ....
}
```

you can dynamically add it to a class as a method (called `resolveThisMethodDynamically`) like this:

```objc
class_addMethod([self class], @selector(resolveThisMethodDynamically), (IMP) myMethodIMP, "v@:");
```

---

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)