0

Can anyone tell me why I can't return a NSMutableArray there:

-(NSMutableArray)makeServiceRequest:(UIViewController *)sender
{
     NSMutableString *urlString= [NSString stringWithFormat:@"http://www.servicedata/%@", _searchValue];

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: urlString]];

        NSError* error;

        NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data
                                                               options:kNilOptions
                                                                 error:&error];

        dispatch_async(dispatch_get_main_queue(), ^(){
            NSLog(@"json arrived");
            return json;
        });
    });
}

I've it well declared on the header file. And is there any way to turn that into a class method?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
88fsantos
  • 393
  • 1
  • 7
  • 22
  • might be you can create a array property and set that inside the method, later through out your class you can use that. – rishi Jun 01 '12 at 16:10

4 Answers4

3

json is a (NSMutableArray *) while your function returns (NSMutableArray)

Stefan
  • 109,145
  • 14
  • 143
  • 218
Anila
  • 1,136
  • 2
  • 18
  • 42
3

You cant do this, you dispatch block is equivalent to creating another thread and letting it go, by the time your block executes the method makeServiceRequest has already executed and is no longer in scope, what you need to do it notify the caller that their request is done via protocols or notifications... the flow goes like

  • Caller calls the method
  • Method executes, starts the block on a different thread and returns
  • Caller keeps going about his business
  • Eventually the block is executed, to get back to the caller we need to use either protocols or notifications (ther are other ways but those two are the best imo)

If you want to learn about NSNotifications check out this questions

Community
  • 1
  • 1
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Protocols or notifications? i've to read about that. I've comed from front-end and i'm still learning oc (10x more difficult). Tks a lot! (if you could tell me how to send a simple notification, to my other method start running, you would be the best!) – 88fsantos Jun 01 '12 at 16:12
  • tks man! you are the best! that points really help me understanding the flow of that thing. – 88fsantos Jun 01 '12 at 16:24
  • just one question. the notification method (observer) is allways on? – 88fsantos Jun 01 '12 at 16:44
  • @88fsantos you will always have the [NSNotificationCenter defaultCenter] and you can always add and remove observers yes.. – Daniel Jun 01 '12 at 16:49
1

That return statement is inside of a block. Think of a block as its own function of sorts. Blocks have arguments and return types, just like a function or method. dispatch_async takes in no arguments and also has no return type, so it won't like you trying to return within that block.

Also, that block is probably going to run on the next run loop. You'll have exited the makeServiceRequest: method by the time that code is run, and nothing will get returned from that method. I'm guessing there is a warning in that code that says something like "Control reaches end of non-void function".

Christian
  • 1,714
  • 8
  • 9
  • hummm i see. i'm a little confuse to follow you guys, but tks a lot! – 88fsantos Jun 01 '12 at 16:19
  • In fact i can get the awser and make a NSlog to the response, even after i'm in another viewcontroller. but all my objects in that second viewcontroller turns into null. And i cant fill the text labels with the json objects. But i can make a NSlog of the json objects. – 88fsantos Jun 01 '12 at 16:22
-1

Like Anila explained, you missed a *. To understand what this means you should read some "getting started" on Objective-C stuff. For example Apple's Getting started tutorial on Objective-C: Learning Objective-C: A Primer

And there is anyway to turn that into a class method?

And to answer you second question, if I got that right: You have to change the dash at the beginning to a plus. This is equivalent to the keyword "static" in Java:

+ (NSMutableArray*)makeServiceRequest:(UIViewController *)sender
Community
  • 1
  • 1
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129