NSMutableData消失(NSMutableData disappearing)

2019-08-03 16:37发布

在我的计划,我有一个收集信息的NSMutableData变量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的大小或者是我的问题只是内存管理?

Answer 1:

您需要为您的XMLDATA变量的属性。 在你之后你的头文件@interface MyClass ,做一个像这样

@property (nonatomic, retain) NSMutableData * xmlData;

如果你正在使用ARC你,如果你使用下面的ARC更改强烈保留离开它一样强烈。 当你想用你的变量,你做self.xmlData



文章来源: NSMutableData disappearing