我有一个测试类,试图通过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("")
请问好吗?
Update1:回答评论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-parameters,cookie,header,authentication challenge等)。
答案 0 :(得分:2)
其中一个问题是我无法访问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)
}
}
的委托对象。我通过创建另一个非泛型类型的RemoteDelegate类来提出解决方案,并将其设置为“conn”的委托。它现在有效,但它只是一个解决方法,我仍然在寻找问题的答案。
我的代表班:
class LRestClient<T: LFModel>
这适用于 let delegate = LRestConnectionDelegate()
delegate.credential = credential
delegate.func_done = func_done
connection = NSURLConnection(request:request, delegate:delegate, startImmediately:true)
:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
switch(v.getId())
{
case R.id.troop_button:
gotoTroopScreen();
break;
}
}
public void gotoTroopScreen()
{
Intent intent = new Intent(this, troop.class);
startActivity(intent);
}
答案 1 :(得分:-1)
因为您正在同步使用NSURLConnection,所以必须在主运行循环中安排操作。将以下代码添加到connect
函数的末尾:
conn?.scheduleInRunLoop(NSRunLoop.currentRunLoop, forMode: NSDefaultRunLoopMode)
或者,将startImmediately
设为NO
并致电conn?.start()
。