ios - NSMutableData 消失

标签 ios nsmutabledata

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

- (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 的大小是否有限制,或者我的问题只是内存管理?

最佳答案

您需要为 xmlData 变量创建一个属性。在你的头文件中之后 @interface MyClass,像这样制作一个

@property (nonatomic, retain) NSMutableData * xmlData;

如果您使用 ARC,则将其保留为强,如果您使用以下 ARC,则将强更改为保留。当您想使用变量时,您可以执行 self.xmlData

关于ios - NSMutableData 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770770/

相关文章:

objective-c - [[NSMutableData alloc] initWithLength :] and CFDataCreateMutable()? 之间的区别

ios - iOS中的UI抽屉?

ios - 带有 Cordova IOS 的 Angular 2 应用程序基础 href

ios - xmppframework - 为发送/接收启用日志

ios - 在 UILabel 中检测 url

ios - 当我点击 2 个 TableViewController 之间 'didSelectRowAt indexPath' 中的单元格时,我需要执行后退按钮操作

ios - 来自 NSURLConnection 的 didReceiveDate 方法警告

ios - 从“NSData *”分配给“NSMutableData *”的指针类型不兼容

ios - NSMutabledata 字节容量

ios - 解码从 HTTP API 请求接收到的数据