views:

118

answers:

1

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!

Update: I moved all of the NSURL functions out of their URLDelegate class and placed them in my controller class. I'm now able to update variables in my controller class from the connectionDidFinishLoading. This seems a bit hacky to me, but it works.

+1  A: 

1. Where should I declare receivedData? My controller class? A third class?

It has to be declared in the same class as that method for that method to use it. From your question, it sounds like that class is your URLDelegate class. Since it's explicitly explained in the comment as “an instance variable”, “elsewhere” must be that class's own header, in the class's instance-variables block.

2. Can I just declare it like any normal NSMutableData variable?

Yes. See the relevant chapter of The Objective-C Programming Language.

3. How do I give my URLDelegate class "access" to this variable?

Classes cannot access instance variables in their instances, since they are instance variables; by definition, they are part of—and, therefore, only accessible to—the instances.

Instances access their instance variables as they would any local or global variable, as demonstrated in the sample code you referred to:

receivedData = [[NSMutableData data] retain];

receivedData is the instance variable you'd declare in your URLDelegate class. Note that you simply use the name “receivedData” by itself; you do not need to use any special syntax to refer to an instance variable rather than another variable. For this reason, avoid giving a instance variable and a local variable the same name.

There are a number of ways you could make the data available to the app controller. A simple one would be to expose a property of the URLDelegate, but that leads to the question of how the app controller should know to retrieve or re-retrieve the value of the property. A more comprehensive list of solutions is in the “Communicating with Objects” chapter of the Cocoa Fundamentals Guide.

Peter Hosey
Thanks very much, Peter. That clears up a lot of confusion.If I'm understanding things correctly, it sounds like I need to figure out two things: 1) how to alert the controller to check for the data, and 2) how to persist the data until the controller has had accessed it.
jthomas
Or, alternatively, how best to pass the data directly to the controller. In fact, it may be best to not even have the URLDelegate class at all; it may be better to make the app controller the connection's delegate.
Peter Hosey