NSMutableData消失了 [英] NSMutableData disappearing

查看:111
本文介绍了NSMutableData消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in my Program, I have a NSMutableData variable that collect the information from http://www.nhara.org/scored_races-2013.htm. After about the third time it gets information from a website, when it contains 90810 bytes, it either disappears or becomes null because if I print it a NSString, it is null. Here is the code

- (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存储数据,但是在声称具有相同的字节数时丢失它。

What confuses me the most is that my NSMutableData stores data, but then loses it while claiming to have the same number of bytes.

NSMutableData的大小是否存在约束或者我的问题只是内存管理?

Is there a constraint to the NSMutableData's size or is my problem just memory management?

推荐答案

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

You need to create a property for your xmlData variable. In your header file after your @interface MyClass, make one like so

@property (nonatomic, retain) NSMutableData * xmlData;

如果您使用ARC,如果您使用ARC以下,则将其保留为强保留。如果你想使用你的变量,你可以 self.xmlData

If you are using ARC you leave it as strong if you using below ARC you change strong to retain. When you want to use your variable you do self.xmlData

这篇关于NSMutableData消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆