NSURLConnection委托方法在通用类中未被调用
我有一个测试类,尝试使用NSURLConnection
访问Google。如果我试图使它通用,NSURLConnectionDataDelegate方法永远不会被调用。NSURLConnection委托方法在通用类中未被调用
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,你得到一个数组(或错误)与一些反射对象,而不是Dictionary。它来自我的LSwift库,并支持各种身份验证方法(buildin参数,cookie,头文件,身份验证挑战等)。
因为您同步使用NSURLConnection,所以必须在主运行循环中安排操作。下面的代码添加到您的connect
函数的末尾:
conn?.scheduleInRunLoop(NSRunLoop.currentRunLoop, forMode: NSDefaultRunLoopMode)
另外,设置startImmediately
到NO
,并呼吁conn?.start()
。
它与这个问题无关,但是如果你遇到类似的问题(不调用'NSURLConnectionDataDelegate'),你可以试试这个。你也可以尝试移动conn,使其成为一个类属性。然而,这个问题只是关于泛型类型。 –
不,不是。他想知道为什么他的代表方法没有被调用。这很可能是因为通过在运行循环中安排连接而未启动连接。 –
对不起,我是谁问了,有或没有'
其中一个问题是我无法访问NSURLConnection
的委托对象。我通过创建另一个RemoteDelegate类来创建解决方案,该类不是泛型类型,并将其设置为“conn”的委托。它现在可以工作,但它只是一个解决方法,我仍然在寻找问题的答案。
我的委托类:
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 7+,也许最好先用'NSURLSession'而不是'NSURLConnection'来开始。我打算使用'NSURLSession'来重写所有连接代码,以支持响应正文,而HTTP代码不是2xx,这是一些服务器所需要的。 –
什么是你创建一个通用类目的是什么?你用这种方法试图解决什么问题? – rdelmar
我添加了一个演示update1。它比CoreData更容易使用 - 主要思想是,在大多数情况下,我们只需要简单的技术,如JSON,Parse等,而XML,AWS等解决方案太复杂。然而,为了构建简单的解决方案,有时我们需要编写一些不常见的代码。 BTW'swift'比'objc'更不灵活,但我只是喜欢'swift'的语法。 –