1

I'm trying to set a BOOL value which is defined in my class inside a block, but I can't see to be able to set it. This is the code.

 __weak __block SPTween *tween2weak = tween;
    __block BOOL buttonScroll2 = buttonScroll;

    tween.onComplete = ^{
        [Sparrow.juggler removeObject:tween2weak];
        buttonScroll2 = NO;
    };

I presume when I do buttonScroll2 = NO, all Im doing is setting a separate variable and not the original, but how do I get to the original from inside the block then?

Phil
  • 2,995
  • 6
  • 40
  • 67

1 Answers1

1

You are correct, once the value of buttonScroll gets copied into buttonScroll2, the changes to the buttonScroll2 have no effect on the original buttonScroll.

If buttonScroll is an instance variable of your object, you should be able to access it using the __weak self pattern:

__weak __block SPTween *tween2weak = tween;
__weak typeof(self) weakSelf = self;
tween.onComplete = ^{
    [Sparrow.juggler removeObject:tween2weak];
    MyClass *strongSelf = weakSelf;
    strongSelf->buttonScroll = NO;
};
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • would weakSelf.buttonScroll work as well? or you have to use -> ? – Phil Jun 03 '14 at 11:52
  • Actually weakSelf->buttonScroll doesn't work as well, it's giving me the error "Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first" – Phil Jun 03 '14 at 11:56
  • @Phil I edited to add a change that the compiler is asking to make. – Sergey Kalinichenko Jun 03 '14 at 12:02
  • Thanks for the help, that really is an area of obj-c that I don't like, too much messing around to do something simple. – Phil Jun 03 '14 at 12:39