0

How to call the original NSError init method when swizzling?

My current implementation

 extension NSError {

    @objc class func errorSwizzle() {
        guard let instance = class_getInstanceMethod(self, #selector(NSError.init(domain:code:userInfo:))),
            let swizzleInstance = class_getInstanceMethod(self, #selector(NSError.init(swizzleDomain:code:info:))) else { return }
        method_exchangeImplementations(instance, swizzleInstance)
    }

    @objc class func errorUnSwizzle() {
        guard let instance = class_getInstanceMethod(self, #selector(NSError.init(domain:code:userInfo:))),
            let swizzleInstance = class_getInstanceMethod(self, #selector(NSError.init(swizzleDomain:code:info:))) else { return }
        method_exchangeImplementations(swizzleInstance, instance)
    }

    @objc convenience init(swizzleDomain: String, code: Int, info: [String : Any]?) {

        /// infinite loop as it calls the swizzled init again.
        self.init(domain: swizzleDomain, code: code, userInfo: info)

        /// Do something.. 
    }
}
Tal Zion
  • 6,308
  • 3
  • 50
  • 73

1 Answers1

2

You have exchanged the implementations, that means you have to call:

@objc convenience init(swizzleDomain: String, code: Int, info: [String : Any]?) {
    self.init(swizzleDomain: swizzleDomain, code: code, info: info)
}

Because self.init(swizzleDomain:...) will contain the original initializer.

Sulthan
  • 128,090
  • 22
  • 218
  • 270