How do blocks differ from normal methods and functions in Objective-C?

后端 未结 2 1627
栀梦
栀梦 2021-02-02 17:32

What is the advantage of using blocks over normal methods and functions in Objective-C? I\'ve read the documentation, but I can\'t find specific uses of blocks instead of other

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 17:55

    Blocks are a way of wrapping up a piece of code and effectively storing it for later use. A block is commonly used in place of a call back function. Newer APIs in the iPhone SDK use blocks this way. The API will take a "block" of code that it will run on completion.

    It saves you having to create your own threads and maintain the state of each thread, manage locks, setup autorelease pools etc.

    When used with the Grand Central Dispatch (GCD) API blocks can be run on queues and entire portions of code can be made to run asynchronously with very little effort, but still keeping the robustness that is required for multithreaded code.

提交回复
热议问题