6

When using @weakify, I get error unexpected '@' in the program. Am I missing some .h files? I already imported ReactiveCocoa.h. Is there any thing I should do ?

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _isSeperateFill = YES;
        _isBorderStroke = NO;
        _isSeperatedStroke = YES;

        _contentWidth = 0;

        @weakify(self);
        [RACObserve(self, dataVO) subscribeNext:^(TableDataVO* dataVO){
            if( dataVO ){
                NSString* indexKey = [[dataVO.tableDataDictionary allKeys] objectAtIndex:0];

                _keys = [dataVO.tableDataDictionary allKeys];
                @strongify(self);
                _rows = [[self.dataVO.tableDataDictionary objectForKey:indexKey] count];
                @strongify(self);
                [self.styleVO setTableHeaderLineHorizontalMargin:self.styleVO.tableWidth / [_keys count]];
            }
        }];

        @weakify(self);
        [RACObserve(self, styleVO) subscribeNext:^(TableStyleVO* styleVO){
            if( styleVO ){
                styleVO.tableHeaderLineHorizontalMargin = styleVO.tableWidth / [_keys count] / 2;
            }
        }];

    }
    return self;
}
benoitcn
  • 149
  • 1
  • 12

2 Answers2

5

@weakify, @strongify and friends are part of libextobjc, not ReactiveCocoa proper.

Try adding this line (per @chakming's comment):

#import "ReactiveCocoa/RACEXTScope.h"

Or for pre-2.3.1 ReactiveCocoa (my original answer), use:

#import <ReactiveCocoa/EXTScope.h>

Rob Bajorek
  • 6,382
  • 7
  • 44
  • 51
1

Not strictly related to the question at hand, but I thought I'd comment and mention that your use and placement of weakify and strongify is incorrect. The correct version would be...

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _isSeperateFill = YES;
        _isBorderStroke = NO;
        _isSeperatedStroke = YES;

        _contentWidth = 0;

        @weakify(self);
        [RACObserve(self, dataVO) subscribeNext:^(TableDataVO* dataVO){
            @strongify(self);
            if( dataVO ){
                NSString* indexKey = [[dataVO.tableDataDictionary allKeys] objectAtIndex:0];
                _keys = [dataVO.tableDataDictionary allKeys];
                _rows = [[self.dataVO.tableDataDictionary objectForKey:indexKey] count];
                [self.styleVO setTableHeaderLineHorizontalMargin:self.styleVO.tableWidth / [_keys count]];
            }
        }];

        [RACObserve(self, styleVO) subscribeNext:^(TableStyleVO* styleVO){
            if( styleVO ){
                styleVO.tableHeaderLineHorizontalMargin = styleVO.tableWidth / [_keys count] / 2;
            }
        }];

    }
    return self;
}

As you can see above, you only need to use weakify once. Once defined it's defined for the entire scope.

Similarly, strongify is only needed once in a block. Further, it's needed at the top of the block so you recapture the weak reference as a strong reference as soon as possible.

By the time it's used in your example it's possible that the captured weak reference to self could have gone out of scope.

Michael Long
  • 1,046
  • 8
  • 15