سؤال

suppose I do this:

MyClass *vista = [[MyClass alloc] initWithFrame:CGRectZero];

vista.onFinish = ^{
    CGRect rect = vista.bounds;
    // bla bla bla
};

then xcode will award me with this error: capturing vista strongly in this block is likely to lead to a retain cycle

Someone suggested declaring vista like this

__unsafe_unretained MyClass *vista = [[MyClass alloc] initWithFrame:CGRectZero];

but the problem is this. vista variable is assigned to a property a few lines down.

self.myVista = vista;

and this is a nonatomic, strong property.

How do I solve that? I can declare a temporary id var to use that, but this appears to be a lame solution.

Any thought?

هل كانت مفيدة؟

المحلول

Try this:

MyClass *vista = [[MyClass alloc] initWithFrame:CGRectZero];

__weak MyClass *weakVista = vista;    
vista.onFinish = ^{
    CGRect rect = weakVista.bounds;
    // bla bla bla
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top