2010-01-15 53 views
1

HI, 我使用的示例代碼從來自鏈路multiple url connectionNSURLConnection問題?

處理多個NSURLConnection的inwhich我所指定的一個的方法,以使

CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag]; 

通過以下

當我使用CustomURLConnection作爲NSObject的
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString *)_tag 

{ 
connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:startImmediately]; 
self.tag = _tag; 
return self; 
    } 

它給出錯誤?

+0

這是什麼錯誤?! – 2010-01-15 08:49:05

回答

1

我想你應該嘗試:

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString *)_tag 
{ 
    if(self = [super initWithRequest:request delegate:delegate]) 
    { 
     self.tag = _tag; 
    } 
    return self; 
    } 

(如果我得到你的權利和CustomURLConnection擴展NSURLConnection的,你貼的代碼是CustomURLConnection的初始化代碼。)

+0

senthilmuthu,看到原始實施在這裏:http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/ – Costique 2010-01-15 09:20:43

+0

看來,在他的情況下CustomURLConnection繼承自NSObject而不是NSURLConnection。 – FelixLam 2010-01-15 09:32:29

+0

是的,在他的描述中錯過了這個。所以我認爲你的回答是對的。 – 2010-01-15 09:49:55

2

的問題是,有沒有self 。如果從NSObjectCustomURLConnection繼承方法應該是這樣的:

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString *)_tag { 
     if(self = [super init]) { 
     self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:startImmediately] autorelease]; 
     self.tag = _tag; 
     } 
     return self; 
} 

你也應該確保connection是類伊娃和dealloc得到正確釋放。同爲tag確保@implementation後添加

@synthesize tag,connection; 

,並申報了Ivar標籤和屬性。

+0

我編輯了代碼。確保合成標籤和連接。 – FelixLam 2010-01-15 10:31:41