ios - Replacing Delegates with Blocks -


i have started learning objective c , came across blocks/closures. annonymous inner classes in java me. have read somewhere blocks can used replace delegates. confuses me in case of delegates signal method when specific task completes, how can delegates replaced blocks?

for example in java, delegates like:

button.addclicklistener(new buttonclickevent(){      void foo(){        // code     } }); 

in case buttonclickevent protocol or in java terms interface. how can represented in terms of blocks in objective c?

if asking if object conforming specific delegate protocol has been set delegate in other object, can replaced block after simple fiddling, i'd answer negative.

blocks sort of enhanced function pointers can used callbacks. there no reciprocal concept in java, not java8's function objects, because actual object single method (i'm sure familiar concept of functional interfaces).

but doesn't mean blocks cannot used callbacks respond events, you'll need sort of adapter forward usual delegate method specified specific delegate protocol callback block have configured.

an extremely nice example of building represented library reactivecocoa, using able this:

self.button.rac_command = [[raccommand alloc] initwithsignalblock:^(id _) {     nslog(@"button pressed!");     return [racsignal empty]; }]; 

i'm not explaining here how works (racsignal abstraction represent stream of events), guess can gist of does, compact.

update:

for more info on how blocks implemented in foundation sdk, see this post. check out post mike ash sample usage scenarios.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -