2009-10-27 104 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