Why can't I return an object from my block?

前端 未结 3 1491
春和景丽
春和景丽 2020-12-11 13:24

I\'m trying to return a NSMutableArray from my block in my method, but when I add the line return [responseString JSONValue];, I get some compiler

3条回答
  •  余生分开走
    2020-12-11 14:13

    The block is defined as void. Think about how you are trying to interact with the block. What are you trying to return from the block? Who would it return to? How?

    I think one misunderstanding you are having is dealing with asynchronous programming. Your method - (NSMutableArray*)getTodayData:(NSDate*)today is actually only sending a request. It is not returning an array. You send the request, and the request will execute the block once it is completed. But your program will have already returned from your getTodayData: method by the time the block gets executed. It will have no return value.

    What you want to do instead is to use a delegate. When the block is executed at the completion of the request, you can notify the delegate that the response is now stored somewhere—preferably as an instance variable—and is now ready to be accessed.

提交回复
热议问题