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

# class_replaceMethod(_:_:_:_:)

Replaces the implementation of a method for a given class.

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

## Parameters

`cls`

The class you want to modify.

`name`

A selector that identifies the method whose implementation you want to replace.

`imp`

The new implementation for the method identified by `name` for the class identified by `cls`.

`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

The previous implementation of the method identified by `name` for the class identified by `cls`.

## Discussion

This function behaves in two different ways:

- If the method identified by `name` does not yet exist, it is added as if [`class_addMethod(_:_:_:_:)`](/documentation/ObjectiveC/class_addMethod(_:_:_:_:)) were called. The type encoding specified by `types` is used as given.
- If the method identified by `name` does exist, its IMP is replaced as if [`method_setImplementation(_:_:)`](/documentation/ObjectiveC/method_setImplementation(_:_:)) were called. The type encoding specified by `types` is ignored.

---

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)