简体   繁体   中英

Can't return NSMutableArray value

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?

json(NSMutableArray *)而您的函数返回(NSMutableArray)

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

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM