0

Is there any way we can have a method that returns a Block? I have two activity objects to whom I have to send the same completion block. In the completion block I am using the reference to the MyTester object also. I wanted to have the block code at a single place only so that if there is any change in it, it could be changed at one place itself.

__block MyTester *aBlockSelf = self;
self.myActivity1.completionBlock = ^(NSDictionary *iData) {
                if (([[iData valueForKey:@"value"] length] > 0)) {
                    [aBlockSelf handleThis:iData];
                } else {
                    // Some Logic
                }

                // Some Logic
            };

self.myActivity2.completionBlock = ^(NSDictionary *iData) {
                if (([[iData valueForKey:@"value"] length] > 0)) {
                    [aBlockSelf handleThis:iData];
                } else {
                    // Some Logic
                }

                // Some Logic
            };
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Why not just write `void (^completionBlock) (NSDictionary *) = ^(NSDictionary *iData) { //... };` and then set both `myActivity1.completionBlock` and `myAcitivity2.completionBlock` equal to `completionBlock`? – Nate Chandler Apr 12 '13 at 00:16
  • They are two different objects and so they need to be set with these values. – Abhinav Apr 12 '13 at 00:19
  • @Abhinav: That doesn't really answer why you can't just store the block into a variable and then set both objects' `completionBlock` to it from there. – Peter Hosey Apr 12 '13 at 00:55
  • Because I want to use the self object in the block. Check this --> [aBlockSelf handleThis:iData]; – Abhinav Apr 12 '13 at 01:12
  • @Abhinav: `aBlockSelf` is the same object in both cases. (Unless these two pieces of code are in different code paths, which, if true, is an important detail that you left out of your question.) – Peter Hosey Apr 12 '13 at 05:13

0 Answers0