views:

20

answers:

1

I'm currently trying to write an iPhone companion to a website that I'm developing. I'm trying to get JSON data from my website by making a controller a delegate of the NSURLConnection. Here's the problem though, I have an NSMutableData object named responseData initialized like so:

responseData = [NSMutableData dataWithLength:0];

And I want to append data as it comes:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

However, this causes my app to crash and says in the console:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[__NSCFArray appendData:]: unrecognized selector sent to instance 
0x6d0a640'

I'm pretty confident that appendData should be recognized, so I'm at a loss as to what the problem is here...

+5  A: 

You have to retain the object:

responseData = [NSMutableData dataWithLength:0];

[responseData retain];

But that's not the common way of doing this. Simply use alloc/init:

responseData = [[NSMutableData alloc] init];

But don't forget to release in dealloc:

[responseData release];
Philippe Leybaert