2009-10-27 105 views
1
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",theXML);  
    [self actualString:theXML 
      extractMyData:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetCategoryResponse xmlns=\"http://tempuri.org/\"><GetCategoryResult>" 
      endingString:@"</GetCategoryResult></GetCategoryResponse></soap:Body></soap:Envelope>" 
      emptyString:@"<Prop_Category />"]; 
    [theXML writeToFile:[self GetMyFilePath] atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil]; 
    [theXML release]; 
} 

-(NSArray*)actualString:(NSString*)theXML extractMyData:(NSString*)prefixString endingString:(NSString*)suffixString emptyString:(NSString*)noDataFromXMLString{ 
    // now here I want to extract data from string 
    // from theXML 
} 

-(NSString*)GetMyFilePath{ 
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *documentDirectory=[paths objectAtIndex:0]; 
    NSString *pathToUserCopyofplist=[documentDirectory stringByAppendingPathComponent:@"myWebServiceData.plist"]; 
    NSLog(@"%@",pathToUserCopyofplist); 
    return pathToUserCopyofplist; 
} 

我想保存到一個plist文件的asp.net web服務的響應。NSString的最大長度是多少?替代NSString - iPhone

但有時,當響應可能是一個巨大的大小。在這種情況下,連接已經收到超過50000字節的數據。 &當我NSlogs NSString - 它打印(空)。

在這種情況下,我無法將網絡服務響應存儲到文件中。

這應該是什麼解決方案? 是否有任何替代方式的NSString? (爲此目的)

在此先感謝。

薩加爾。

回答

4

如果你想存儲的東西,你已經擁有的字節數:[myWebData mutableBytes]

+0

您可以創建一個帶有字節,可變或不可變的文件。請參閱http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/uid/20000172-CIAEAHFJ以獲取使用的一種方法。 – 2009-10-27 18:22:11

+0

您可以生成「」的字節等價物,並在可變數據對象中查找關聯的字節範圍。然後,您可以用NULL替換這些字節:http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/Reference/NSMutableData.html#//apple_ref/doc/uid/20000173 -BGBBIABA – 2009-10-27 18:43:50

+0

先生,但是,在這裏我很困惑。無論Web服務如何迴應,我都想刪除問題中給出的部分內容。我的問題是「是否可以使用nsstring?」 「更可取的是可變數據?」 「如何用nsmutable數據替換它?」 – 2009-10-27 18:52:03

1

這是一個移動設備,切記。內存是非常有限的,當你說你可能想要分配超過500000字節時,它會給我一個紅旗。這對你來說是一個問題。

如果文件大小超過n,考慮使用流式或塊算法。也許你的網絡服務可以將響應分成合理的(並且已知的最大)大小和設備的部分,並在接收它們時將它們寫入文件?

+0

對不起。但是我錯誤地加了一個零。 – 2009-10-27 18:03:02

+0

啊。 50 K越來越好:) – marcc 2009-10-27 18:20:02

+0

先生,我更新了更多細節。這清楚地說明了爲什麼我需要使用nsstring格式的數據。 – 2009-10-27 18:43:07