2012-12-08 5 views
0

가능한 중복 : 나는이 NSMutableData 변수가있는 경우는 90,810 바이트가있을 때
NSMutableData dissapearingNSMutabledata 바이트 용량

, 내 프로그램에서 항상 내용을 덤프합니다. 데이터의 바이트 수가 데이터를 잃어 버리게합니까? 여기에 코드가 있습니다. 이 잘릴 경우 여기

- (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]; 


    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에 대한 설명이 도움이 작동하지 않는 이유는 스탠드에서 내가하려고

<td style="font-size: 12.0pt; color: black; font-weight: 400; text-decoration: none; text-underline-style: none; font-family: Arial Narrow; font-style: normal; text-align: general; vertical-align: bottom; white-space: nowrap; border-left: medium none; border-right: 1.0pt solid windowtext; border-top: medium none; border-bottom: 1.0pt solid windowtext; padding-left: 1px; padding-right: 1px; padding-top: 1px; background: #EAF1DD"> 
    King Pine</td> 
    <td style="font-size: 12.0pt; color: black; font-weight: 400; text-decoration: none; text-underline-style: none; font-family: Arial Narrow; text-align: right; font-style: normal; vertical-align: bottom; white-space: nowrap; border-left: medium none; border-right: 1.0pt solid windowtext; border-top: medium none; bor 
2012-12-07 19:35:48.652 race tracker[2357:c07] xmlCheck = (null) 
2012-12-07 19:43:28.914 race tracker[2357:c07] xmlCheck2 = (null) 
2012-12-07 19:43:28.921 race tracker[2357:c07] (
) 

을 변경합니다.

+0

몇 시간 전에 질문했던 것과 똑같은 질문을 반복하지 마십시오. – rmaddy

답변

1

NSMutableData은 임의의 크기로 커집니다.

curl http://www.nhara.org/scored_races-2013.htm > file.txt을 실행하면 서버에서 반환하는 파일의 길이가 정확히 90810 바이트임을 알 수 있습니다. 그래서 당신은 그 바이트 수를 다시 얻습니다. 채워지는 가변 데이터 객체가 아니라 90810이 정확히 올바른 길이입니다.

귀하의 문제는 여기에 있습니다 :

[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] 

서버가 반환되는 문자열은 UTF-8이 유효하지 않습니다. 이 문제를 직접 확인하려면 NSASCIIStringEncoding (모든 바이트가 유효한 인코딩)을 시도해보십시오. 문자열이 올바르게 누적되는 것을 볼 수 있습니다.

반환 된 파일을 검색 한 후 특정 문제는 위치 74,963의 바이트 - 활자체 작은 따옴표로 사용 된 0x92입니다. NSWindowsCP1252StringEncoding을 원하는 것처럼 보입니다. HTML을 업로드 한 사람은 Microsoft의 코드 페이지 1252를 사용하는 것으로 보입니다.