Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reassigning blocks objective-c

The block is a property defined in GridScrollView:

typedef BoxView* (^RenderBlock)(NSDictionary* json, CGRect);
@interface GridScrollView : PagingScrollView
@property (nonatomic, copy) RenderBlock renderBlock;

I want to use it like this:

switch(current.tag)
{
    case 1:
        scrollView.renderBlock = ^(NSDictionary* json, CGRect frame)
        {
             //returns a boxview
        }
        break;
     case 2:
        scrollView.renderBlock = ^(NSDictionary* json, CGRect frame)
        {
             //returns a different boxview
        }
        break;
}

While this code works fine the first time around, when it gets reassigned I get an EXC_BAD_ACCESS (code=2, address=0x0) error. What's happening here?

like image 834
taimur38 Avatar asked Dec 04 '25 17:12

taimur38


1 Answers

Since the call to the block is itself performing the declaration of an object, try adding an extra pair of braces around it:

case 1: {
            scrollView.renderBlock = ^(NSDictionary* json, CGRect frame) {
             //returns a boxview
            }
        }
        break;

Although I don't know why it would run as is the first time and then crash.

like image 139
Wienke Avatar answered Dec 06 '25 11:12

Wienke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!