On Dec 6, 2013, at 11:37 AM, Jens Alfke <j...@mooseyard.com> wrote:
> On Dec 6, 2013, at 7:27 AM, Graham Cox <graham....@bigpond.com> wrote:
>> Is the value of <tileRect> here captured when the block is created,or when 
>> it is run?
> 
> It depends on whether tileRect is an instance variable.
> * If it isn’t (i.e. it’s local/static/global), it gets captured when the 
> block is created.
> * If it _is_ an ivar, then “tileRect” is just syntactic sugar for 
> “self->tileRect”, which means that ‘self’ gets captured at create time, and 
> the ‘->tileRect’ part is evaluated at runtime.

*Only* ordinary local variables are captured when the block is constructed. 
Globals, static locals, __block locals, and ivars are not captured.


% clang test.m -framework Foundation && ./a.out
local 0, static_local 1, block_local 1, global 1, ivar 1

% cat test.m
#include <Foundation/Foundation.h>

@interface Test : NSObject @end

int global;

@implementation Test {
    int ivar;
}

-(void)method 
{
    int local;
    static int static_local;
    __block int block_local;

    local = 0;
    static_local = 0;
    block_local = 0;
    global = 0;
    ivar = 0;

    void (^block)(void) = ^{ 
        printf("local %d, static_local %d, block_local %d, global %d, ivar 
%d\n", 
               local, static_local, block_local, global, ivar);
    };

    local = 1;
    static_local = 1;
    block_local = 1;
    global = 1;
    ivar = 1;

    block();
}

@end

int main()
{
    [[Test new] method];
}


-- 
Greg Parker     gpar...@apple.com     Runtime Wrangler



_______________________________________________

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