2010-08-22 27 views
2

的didReceiveData方法奇怪的問題,我在植入簡單NSURLConnection的得到了這個奇怪的問題......與appendData在NSURLConnection的

的方法didReceiveData獲得打電話,我高興地試圖追加接收數據,但...虛無縹緲!

有是肯定的一些數據(如長度指示,但appendData沒有附加數據!

我開始砰我的頭就這一個,我需要一些幫助,它的後期:-)

這裏是一些代碼來看看:

我的頭......

@interface ActionViewController : UITableViewController { 
Site *site; 

NSURLConnection *siteConnection; 
NSMutableData *receivedData; 
    UIView *waitView; 
    UIActivityIndicatorView *activityIndicator; 

int nConnections; 
    BOOL fail; 
} 

我的實現..

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the new data to receivedData. 
NSLog(@"Received %d bytes of data",[data length]); 
    [receivedData appendData:data]; 
NSLog(@"Received %d bytes of data",[receivedData length]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
[activityIndicator stopAnimating]; 
waitView.hidden = YES; 

    // release the connection 
    [connection release]; 
} 

控制檯輸出...

[Session started at 2010-08-21 21:27:55 -0400.] 
2010-08-21 21:28:19.263 myApp[2042:207] Received 108 bytes of data 
2010-08-21 21:28:19.263 myApp[2042:207] Received 0 bytes of data 
2010-08-21 21:28:19.263 myApp[2042:207] Succeeded! Received 0 bytes of data 

我不明白這一點!幫幫我!!!

BTW,該數據是一個簡單的XML結果是這個樣子......

<donnee>0</donnee><donnee>0</donnee><donnee>0</donnee><donnee>1</donnee><donnee>0</donnee><donnee>0</donnee> 

回答

12

出色,得到我的答案在本網站上的另一個問題有關NSMutableData ......忘了初始化的東西! (代碼18或發送消息的其餘部分爲nil是Objective-C的NICE特性)

receivedData = [[NSMutableData alloc] init]; 
+2

謝謝,這個同樣的問題正在導致我死亡。 – cksubs 2011-12-02 05:22:42

+0

啊另一個例子就是我的愛與恨的關係與iOS的悄然失敗的方式。這裏沒什麼可看的,這些不是你要找的機器人。 – Rydell 2013-02-02 21:59:37