我已經創建了一個類,用於在基於NSURLConnection的iOS-App中獲取服務器端數據,將獲取的數據轉發給委託進行處理。xcode NSURLConnection NSMutableData appendData僅適用於某些類型的內容
當下載文本/ JSON這個完美的作品。
我現在正在擴展獲取一些PNG圖像,無法獲取數據。
我發現它在didReceiveData-call中斷,其中[receivedData appendData:data]對接收到的圖像數據不做任何處理。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
[receivedData appendData:data];
}
我得到數據到這個方法(NSData的> 0字節),但調用[receivedData appendData:數據]不改變receivedData(靜像0字節)的大小。
由於這與文本完美配合,而不是與圖像數據相關,我認爲它與字符集或編碼有關,但無法在其上找到任何內容。
任何幫助表示讚賞。
更新與請求的代碼樣本:
在使呼叫的方法:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [NSMutableData data];
// Also tried with no change in behaviour
// receivedData = [[NSMutableData alloc] initWithLength:0];
} else {
// Inform the user that the connection failed.
}
我didReceiveResponse
:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
另一個更新
原來,問題BOIS下來併發我火類的三個實例同時放至asynchroniously取三個文件。問題是第一個版本receivedData
在完成破壞來自其他兩個實例的響應時。
如何確保每個課程實例都有自己的receivedData
玩?
最後說明 使用MutableArrays爲retreived數據的詞典解決併發問題。
它也好像定義MutableArray作爲類的私有屬性爲每個實例創建一個數組。
@interface MyClass(){
@private NSMutableData * receivedData;
}
我想'receivedData'只是'nil'。 – omz
Plz向我們展示您的應用程序中的「 - (void)連接:(NSURLConnection *)連接didReceiveResponse:(NSURLResponse *)響應 」方法。 –
'連接didReceiveResponse'只需調用'[receivedData setLength:0]' – mariusnn