Objective-C Blocks and variable scope

后端 未结 2 732
北荒
北荒 2020-12-21 09:03

I would like to set the value of an NSData object within my block. Can someone let me know what I have done wrong here?

// Data
__block NSData *         


        
2条回答
  •  一整个雨季
    2020-12-21 09:23

    performRequestWithUrl:xmlString:completionHandler: is not a synchronous method. It sets up an asynchronous request and then immediately returns. The rest of your method runs and returns.

    When the request completes, only then does it run your block, which assigns data and then immediately throws it away.

    Were this a synchronous method, your routine would block pending an expensive network operation. Were this on the main thread, your entire app would hang. ZSURLConnection (and NSURLConnection) is designed to avoid that.

提交回复
热议问题