Is there an advantage to using blocks over functions in Objective-C?

前端 未结 2 1667
既然无缘
既然无缘 2021-01-13 05:01

I know that a block is a reusable chunk of executable code in Objective-C. Is there a reason I shouldn\'t put that same chunk of code in a function and just called the funct

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 05:31

    As Brad Larson comments in response to this answer:

    Blocks will let you define actions that take place in response to an event, but rather than have you write a separate method or function, they allow you to write the handling code right where you set up the listener for that event. This can save a mess of code and make your application much more organized.

    A good example of which i can give you is of alert view, it will be good if i decided at time of creation of alert view what will happen when i dismiss that instead i write the delegate method and wait for that to call. So it will be much easier to understand and implement and also it provides fast processing.

提交回复
热议问题