我想混用URLSession类方法dataTask,但没能完成。
我在objective-c中尝试了这种方法,它工作得很好,因为现在我想在swift 3中实现它,但没有得到任何东西。
我提到了this链接,但它在swift 3中不起作用。
请帮帮我。
发布于 2017-05-01 19:31:18
我们可以像下面这样在swift中混合使用方法
let selectorfirst = #selector(ClassName.function(_:))
let swizzledFunction = #selector(ClassName.function2(_:))
let method1 = class_getInstanceMethod(ClassInstance, selectorfirst)
let method2 = class_getInstanceMethod(ClassInstance, swizzledFunction)
method_exchangeImplementations(method1, method2) //尝试使用subclass
class URLSessionSubClass: URLSession {
static var sharedInstance: URLSessionSubClass?
static func getSharedInstance() -> URLSessionSubClass {
if sharedInstance == nil {
sharedInstance = URLSessionSubClass()
let selectorfirst = #selector(URLSessionSubClass.function(_:))
let swizzledFunction = #selector(URLSessionSubClass.function2(_:))
let method1 = class_getInstanceMethod(self, selectorfirst)
let method2 = class_getInstanceMethod(self, swizzledFunction)
method_exchangeImplementations(method1, method2)
}
return sharedInstance!
}
}发布于 2019-06-22 13:49:08
我已经尝试了简单的NSURLSession swizzling,它确实有效,然而,当试图实现swizzling的Alamorfire和其他第三方网络开发工具包,它不起作用,因为我们需要swizzling的基础上,实现URLSessionConfiguration.default与swizzling方法,并添加所有的属性,如startLoading,stopLoading,初始化和canInit功能。为了返回值,还要添加URLSession委托方法。
https://gist.github.com/nil-biribiri/ceead941d5b482207cb1b872c5d76a60
https://stackoverflow.com/questions/43717778
复制相似问题