HomeiOS Developmentios - Technique Swizzling doesn't work when strategies have parameters

ios – Technique Swizzling doesn’t work when strategies have parameters


I’ve been doing methodology swizzling (Goal-C to Swift) for my unit assessments and so they’re completely working, besides when the time got here I have to swizzle strategies with parameters.

Outdated methodology to be swizzled:

@interface FNServerApiRequests : NSObject
+ (void) pullTypesFromServer:(void (^)(id responseJson))completionBlock error:(MKNKErrorBlock)errorBlock;
@finish

Then my new Swift operate:

extension FNServerApiRequests {
  @objc
  class func swizzledPullTypes(fromServer completionBlock: @escaping (Any) -> Void, error errorBlock: @escaping MKNKErrorBlock) {
    print("swizzledPullTypes")
    let dic = FNTestUtils.getJsonDictionaryFile("pull-types.json")
    completionBlock(dic!)
  }
}

Then lastly:

  func setupSwizzling() {
    let originalMethod = class_getClassMethod(FNServerApiRequests.self, #selector(FNServerApiRequests.pullTypes(fromServer:error:)))
    let swizzledMethod = class_getClassMethod(FNServerApiRequests.self, #selector(FNServerApiRequests.swizzledPullTypes(fromServer:error:)))
    method_exchangeImplementations(originalMethod!, swizzledMethod!)
  }

I matched precisely how the 2 strategies are declared in Swift (e.g. parameter names, placeholders, and many others…)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments