views:

389

answers:

2

Hi everybody

I have this simple scenario:

  • main.m (which is my main class)
  • myClass.m (which is an additional class)

In my plans it should happen something like this

(in MAIN.m)

myClass *foo = [[myClass alloc] init];
NSArray *array = [foo returnAnArray];

What myClass does is opening a new NSURLConnection, retrieving some data and - when it's ready - parsing it and returning an array.

To return an array with the parsed data I wrote this simple

(NSArray *) returnArray:(NSString *)dataDownloadedFromWeb

In order to know when the data has ACTUALLY finished downloading and when to start parsing it, I'm using

- (void) connectionDidFinishDownloading: (NSURLConnection *connection)

But WHERE and HOW can I call returnArray: if connectionDidFinishLoading is VOID? :(

I hope this makes sense... I'm really stuck and I cannot seem to find a solution. I tried with NSNotification but it didn't seem to work either.

Thanks everybody for the support! Enrico

A: 

You need to implement connectionDidFinishDownloading yourself. You need to create a delegate object that you pass to NSURLConnection when you create it. The delegate object needs to implement connectionDidFinishDownloading.

The NSURLConnection will then call connectionDidFinishDownloading on your delegate object at the approperiate time.

There's further info here

Tom
That's it! :) DELEGATE was the way to go, I had this suspicioun from the beginning...Thanks for you help, really, you made my day.Still have lots of things to learn about Objective-C...
Chico Jobs
+1  A: 

Presumably you mean - (void)connectionDidFinishLoading:(NSURLConnection *)connection ?

Anyway, you dont wait until it has finished - you parse the data when it is ready in - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

  • connectionDidFinishLoading: is letting you know that there wont be anymore data coming your way.
+1 And as Tom and jib point out, you need to implement these delegate methods yourself to get the callbacks at various stages of the process.
gavinb