Question

Possible Duplicate:
NSMutableData dissapearing

When I have a NSMutableData variable, in my program, it always dumps its content when it has 90810 bytes. Does the number of byte in the data cause the data to be lost? here is the code.

- (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);
}

here is where it get cut off, but that changes

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

I am trying to under stand why my NSMutableData is not working so an explanation of NSMutableData would help.

Was it helpful?

Solution

NSMutableData will grow to an arbitrary size.

Having run curl http://www.nhara.org/scored_races-2013.htm > file.txt, I can tell you that the file the server returns is exactly 90810 bytes long. So that's why you get that number of bytes back. It's not the mutable data object that fills up, it's that 90810 is exactly the right length.

Your problem is here:

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

The string your server is returning isn't valid UTF-8. To verify this for yourself, try NSASCIIStringEncoding (which is an encoding in which all bytes are valid). You'll see that you correctly accumulate a string.

Having searched the file returned, the specific problem is the byte at position 74,963 — the 0x92 used for a typographic single quote. It looks like you want NSWindowsCP1252StringEncoding. Whoever uploaded that HTML appears to have used Microsoft's code page 1252.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top