NSMutableData消失

在我的程序中,我有一个NSMutableDatavariables来收集来自http://www.nhara.org/scored_races-2013.htm的信息。 大约第三次它从网站获取信息,当它包含90810字节,它要么消失或成为空,因为如果我打印它的NSString,它是空的。 这是代码

- (void)viewWillAppear:(BOOL)animated { // Create a new data container for the stuff that comes back from the service xmlData = [[NSMutableData alloc] initWithCapacity:180000]; [self fetchEntries]; [super viewWillAppear:animated]; } - (void)fetchEntries { // Construct a URL that will ask the service for what you want NSURL *url = [NSURL URLWithString: @"http://www.nhara.org/scored_races-2013.htm"];// // Put that URL into an NSURLRequest NSURLRequest *req = [NSURLRequest requestWithURL:url]; // Create a connection that will exchange this request for data from the URL connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; } - (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data { // Add the incoming chunk of data to the container we are keeping // The data always comes in the correct order [xmlData appendData:data]; NSLog(@"%@",xmlData); NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]autorelease]; NSLog(@"xmlCheck = %@", xmlCheck); } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"error= %@",error); } - (void)connectionDidFinishLoading:(NSURLConnection *)conn { // We are just checking to make sure we are getting the XML NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"xmlCheck2 = %@", xmlCheck); } 

最令我困惑的是我的NSMutableData存储数据,但是却声称拥有相同的字节数,然后丢失了它。

是否有一个约束的NSMutableData的大小或者是我的问题只是内存pipe理?

你需要为你的xmlDatavariables创build一个属性。 在你的@interface MyClass之后的头文件中,像这样做一个

 @property (nonatomic, retain) NSMutableData * xmlData; 

如果您使用的是ARC,那么您可以使用ARC,如果您使用的是ARC,那么您需要更改才能保留。 当你想使用你的variables你做self.xmlData