Objective-C and NSURL: where am I supposed to declare receivedData?

Posted by jthomas on Stack Overflow See other posts from Stack Overflow or by jthomas
Published on 2010-06-13T00:57:28Z Indexed on 2010/06/13 1:02 UTC
Read the original article Hit count: 274

I have a two classes, a controller called "AppController" and a class called "URLDelegate" that encapsulates the sample NSURL code from Apple's URL Loading System Programming Guide.

The guide repeatedly mentions declaring the receivedData instance variable "elsewhere." I assume this means outside of the URLDelagate class, because if I declare it in the URLDelegate class, my controller class can't "see" the data that has been downloaded.

I know that data is received, because in my connectionDidFinishLoading function, I have NSLog display the results:

NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
receivedText=[[NSString alloc] initWithData:receivedData encoding: NSASCIIStringEncoding];
NSLog(@"receivedText=%@",receivedText);

So I'm a bit stumped with the following questions:

  1. Where should I declare receivedData? My controller class? A third class?
  2. Can I just declare it like any normal NSMutableData variable?
  3. How do I give my URLDelegate class "access" to this variable? E.g., if I declare receivedData in my AppController class, wouldn't I have to instantiate AppController within URLDelegate? But how would this possible if it's the AppController class which is instantiating the URLDelegate class in the first place?

Especially with regard to the last question, I feel like I must be overlooking something blindingly obvious and fundamental. If anyone could point me in the right direction, I would really appreciate it.

Thank you!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa