---
title: "class_addMethod(_:_:_:_:)"
framework: Objective-C Runtime
role: symbol
role_heading: Function
platforms: [iOS 2.0+, iPadOS 2.0+, Mac Catalyst 13.1+, macOS 10.5+, tvOS 9.0+, visionOS 1.0+, watchOS 1.0+]
path: "objectivec/class_addmethod(_:_:_:_:)"
---

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

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

## Declaration

```swift
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 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048 > 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

Return Value YES if the method was added successfully, otherwise NO (for example, the class already contains a method implementation with that name).

## Discussion

Discussion 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(_:_:). An Objective-C method is simply a C function that take at least two arguments—self and _cmd. For example, given the following function: void myMethodIMP(id self, SEL _cmd) {     // implementation .... } you can dynamically add it to a class as a method (called resolveThisMethodDynamically) like this: class_addMethod([self class], @selector(resolveThisMethodDynamically), (IMP) myMethodIMP, "v@:");

## See Also

### Working with Classes

- [class_getName(_:)](objectivec/class_getname(_:).md)
- [class_getSuperclass(_:)](objectivec/class_getsuperclass(_:).md)
- [class_setSuperclass(_:_:)](objectivec/class_setsuperclass(_:_:).md)
- [class_isMetaClass(_:)](objectivec/class_ismetaclass(_:).md)
- [class_getInstanceSize(_:)](objectivec/class_getinstancesize(_:).md)
- [class_getInstanceVariable(_:_:)](objectivec/class_getinstancevariable(_:_:).md)
- [class_getClassVariable(_:_:)](objectivec/class_getclassvariable(_:_:).md)
- [class_addIvar(_:_:_:_:_:)](objectivec/class_addivar(_:_:_:_:_:).md)
- [class_copyIvarList(_:_:)](objectivec/class_copyivarlist(_:_:).md)
- [class_getIvarLayout(_:)](objectivec/class_getivarlayout(_:).md)
- [class_setIvarLayout(_:_:)](objectivec/class_setivarlayout(_:_:).md)
- [class_getWeakIvarLayout(_:)](objectivec/class_getweakivarlayout(_:).md)
- [class_setWeakIvarLayout(_:_:)](objectivec/class_setweakivarlayout(_:_:).md)
- [class_getProperty(_:_:)](objectivec/class_getproperty(_:_:).md)
- [class_copyPropertyList(_:_:)](objectivec/class_copypropertylist(_:_:).md)
