gpt4 book ai didi

ios - NSURLConnection 委托(delegate)方法未在泛型类中调用

转载 作者:行者123 更新时间:2023-11-28 07:07:31 24 4
gpt4 key购买 nike

我有一个测试类试图通过 NSURLConnection 连接到 google。如果我尝试使其通用,则永远不会调用 NSURLConnectionDataDelegate 方法。

class Remote<T: NSObject>: NSObject, NSURLConnectionDelegate, NSURLConnectionDataDelegate {
//class Remote: NSObject, NSURLConnectionDelegate, NSURLConnectionDataDelegate {

var data = NSMutableData()

func connect(query:NSString) {
var url = NSURL(string:"http://www.google.com")!
var request = NSURLRequest(URL: url)
var conn = NSURLConnection(request: request, delegate: self, startImmediately: true)
}


func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
LF.log("didReceiveResponse")
}

func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
LF.log("didReceiveData")
self.data.appendData(conData)
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
LF.log("didFinished")
//println(self.data)
}


deinit {
println("deiniting")
}
}

测试它(注释/取消注释要比较的第一行/第二行):

    let remote = Remote<NSObject>()
//let remote = Remote()
remote.connect("")

有什么想法吗?

更新 1:回答评论 1,它是一个为您处理网络连接和解析的 REST 客户端。稍后我会写一篇关于此的博客(因为它仍在开发中),但为了让您了解我的项目,这里有一些演示代码:

        let client = ICRestClient<ICCategoryModel>(api:IC.api.category_list)
client.func_array = {
(results: [ICCategoryModel]?, error: NSError?) -> Void in
block!(results, error)
}
client.execute()

ICCategoryModel 就像:

class ICSubCategoryModel: ICModel {
var name: String?
var category_id: Int = 0
}

这个想法是,你传入 API URL,你会得到一个数组(或错误),其中包含一些反射对象而不是字典。它来 self 的 LSwift 库,支持各种身份验证方法(内置参数、cookie、 header 、身份验证挑战等)

最佳答案

问题之一是我无法访问 NSURLConnection 的委托(delegate)对象.我想出了一个解决方案,方法是创建另一个 RemoteDelegate 类,它不是泛型类型,并将其设置为“conn”的委托(delegate)。它目前有效,但它只是一种变通方法,我仍在寻找问题的答案。

我的委托(delegate)类:

class LRestConnectionDelegate: NSObject {

var func_done: ((NSURLResponse?, NSData!, NSError!) -> Void)?
var credential: NSURLCredential?
var response: NSURLResponse?
var data: NSMutableData = NSMutableData()

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if challenge.previousFailureCount > 0 {
challenge.sender.cancelAuthenticationChallenge(challenge)
} else if let credential = credential {
challenge.sender.useCredential(credential, forAuthenticationChallenge:challenge)
} else {
LF.log("REST connection will challenge", connection)
}
}
func connection(connection: NSURLConnection, didReceiveResponse a_response: NSURLResponse) {
//LF.log("CONNECTION response", response)
response = a_response
}
func connection(connection: NSURLConnection, didReceiveData data_received: NSData) {
//LF.log("CONNECTION data", data.length)
data.appendData(data_received)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
//LF.log("CONNECTION finished", connection)
if func_done != nil {
func_done!(response, data, nil)
}
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
//LF.log("CONNECTION failed", error)
if let func_done = func_done {
func_done(response, nil, error)
}
}
deinit {
//LF.log("DELEGATE deinit", self)
}
}

这适用于 class LRestClient<T: LFModel> :

        let delegate = LRestConnectionDelegate()
delegate.credential = credential
delegate.func_done = func_done
connection = NSURLConnection(request:request, delegate:delegate, startImmediately:true)

关于ios - NSURLConnection 委托(delegate)方法未在泛型类中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29886104/

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