At 12:14 PM -0800 1/27/12, Jens Alfke wrote:
I'm really used to using -performSelector:withObject:afterDelay: to make something happen later. But I'd much rather use a block than a target/action.

At 12:23 PM -0500 1/28/12, Steve Sisak wrote:
This reminds me that, IIRC, a block (after it's copied to the heap) _is_ an object. I was going to suggest some form of calling BlockCopy manually, but given the example above, you might something like:

-(void)executeBlock:((^)(void) block  // <- someone help with this declaration
{
  block();
}

This was interesting enough to stick my head in the documentation and build a test program.

The following appears to work:

- (void)performBlock:(void (^)(void)) block
{
    block();
}

- (IBAction) doItNow:(id)sender
{
    [self performSelector:@selector(performBlock:) withObject:^{
        NSLog(@"Done");
    }];
}

- (IBAction) doItLater:(id)sender
{
    [self performSelector:@selector(performBlock:)
               withObject:^{
        NSLog(@"Done Later");
    }   afterDelay:1.0];
}

Does anyone see a problem with this technique?

-Steve
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to