0

私のプログラムには、http://www.nhara.org/scored_races-2013.htmから情報を収集する NSMutableData 変数があります。Web サイトから情報を取得するのは約 3 回目で、90810 バイトが含まれていると、NSString を出力すると null になるため、消えるか 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 のサイズに制約がありますか、それとも私の問題は単なるメモリ管理ですか?

4

1 に答える 1

1

xmlData 変数のプロパティを作成する必要があります。の後のヘッダーファイルで、次の @interface MyClassようにします

@property (nonatomic, retain) NSMutableData * xmlData;

ARCを使用している場合は強いままにし、ARC以下を使用している場合は強いに変更して保持します. 変数を使用したいときはself.xmlData

于 2012-12-07T20:56:27.640 に答える