Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested blocks and references to self

I have a block wherein I use self so I declare a weak reference to self:

__weak MyClass *weakSelf = self;

Now my questions:

  1. I get an error where I define weakSelf and I don't understand what this should mean.:

    weak attribute can not be specified on an automatic variable

  2. Inside my block I pass weakSelf to another block and I am not sure if I now have to do the same thing again like so:

    __weak MyClass *weakWeakSelf = weakSelf;
    

    And then pass weakWeakSelf to that block?

like image 527
Besi Avatar asked May 03 '12 12:05

Besi


People also ask

What are nested blocks?

Block references that contain other blocks are known as nested blocks. Using blocks within blocks can simplify the organization of a complex block definition. The only restriction on nested blocks is that you cannot insert blocks that reference themselves.

What is a nested block Java?

As the name suggests, a try block within a try block is called nested try block in Java. This is needed when different blocks like outer and inner may cause different errors.


2 Answers

This is most likely occurring as you are targeting down to iOS 4. You should change it to be

__unsafe_unretained MyClass *weakWeakSelf = weakSelf;
like image 110
Paul.s Avatar answered Sep 22 '22 01:09

Paul.s


With ARC

__weak __typeof__(self) wself = self;

Wihtout ARC

__unsafe_unretained __typeof__(self) wself = self;
like image 35
hfossli Avatar answered Sep 22 '22 01:09

hfossli