One of these things consistently pukes...
Forgive blown line ends in the code that follows... It's cut-n-pasted
directly from the project, and the newsreader isn't too good at wrapping
Objective-C!
In the code that follows, if "TESTING" = 1, I get the expected result:
The instance variable (declared as "NSMutableData *Buffer;" in my .h
file) Buffer comes back filled with the HTML from MyURL.
If "TESTING" !=1, Buffer comes back filled with random garbage.
Likewise, when the first version gets used, the local NSData (Note NOT
mutable) TheData variable is loaded with the HTML that comes from MyURL,
but when the second version is used, TheData contains random garbage.
In both cases, "TheData" (the "NSData" version that actually
participates in the "read from the URL" operation) *SHOULD*, according
to my understanding based on reading the API help, get stuffed with the
contents of MyURL. In the first version, it does. In the second, it gets
filled with random garbage.
The second is clearly less complex (A whole two statements, versus
five), but doesn't appear to perform "as advertised"...
Why???
Code follows...
#define TESTING 1
#if TESTING // ********** First version
- (void)GetRawData
{
NSError *TheError = [NSError alloc];
NSURLResponse *TheResponse = [NSURLResponse alloc]; // We don't care
about it, but we need it to make the NSURLConnection call
NSURL *MyURL = [NSURL
URLWithString:@[EMAIL PROTECTED]
"http://moneycentral.msn.com/investor/market/exchangerates
..aspx?selRegion=1&selCurrency=1"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:MyURL];
NSData *TheData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&TheResponse error:&TheError];
printf("TheData:\n");
HexDump((UInt8 *)[TheData bytes], [TheData length], 1);
[Buffer setData:TheData];
printf("The Buffer:\n");
HexDump((UInt8 *)[Buffer mutableBytes], [Buffer length], 1);
}
#else // *********** Second version....
- (void)GetRawData
{
NSURL *MyURL = [NSURL
URLWithString:@[EMAIL PROTECTED]
"http://moneycentral.msn.com/investor/market/exchangerates
..aspx?selRegion=1&selCurrency=1"];
NSData *TheData = [NSData dataWithContentsOfURL:MyURL];
printf("TheData:\n");
HexDump((UInt8 *)[TheData bytes], [TheData length], 1);
[Buffer setData:TheData];
printf("The Buffer:\n");
HexDump((UInt8 *)[Buffer mutableBytes], [Buffer length], 1);
}
#endif
--
Don Bruder - dakidd@[EMAIL PROTECTED]
- If your "From:" address isn't on my
whitelist,
or the subject of the message doesn't contain the exact text
"PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without
my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd>
for more
info


|