gpt4 book ai didi

ios - 如何调配 NSURLSession 类方法 dataTaskWithUrl

转载 作者:行者123 更新时间:2023-11-30 13:40:54 31 4
gpt4 key购买 nike

我一直在尝试混合 NSURLSession 类方法 dataTaskWithRequest 但未能完成

extension NSURLSession{
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}

if self !== NSURLSession.self {
return
}

dispatch_once(&Static.token) {
let originalSelector = Selector("dataTaskWithRequest:completionHandler:")
let swizzledSelector = Selector("my_dataTaskWithRequest:completionHandler:")

let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}

// Swizzled Method
func my_dataTaskWithRequest(request: NSURLRequest,completionHandler: (NSData?, NSURLResponse?, NSError?)) -> NSURLSessionDataTask {

print("Inside Swizzled Method")

return my_dataTaskWithRequest(request,completionHandler: completionHandler)
}
}

提前致谢!!

最佳答案

我必须将 JSON(或 NSDictionary )格式的数据上传到服务器。
我做过这样的事情...

let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey
// OR
let urlPath:String = "your url string"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url!, completionHandler: {(data, reponse, error) in
println("Task completed")
// rest of the function...
})

task.resume()


欲了解更多或相关答案,请访问链接
How to swizzle in private method

希望对你有帮助

关于ios - 如何调配 NSURLSession 类方法 dataTaskWithUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570809/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com