When using @weakify get error, unexpectedly "@" in program

When using @weakify, I get the error "@" in the program. Am I missing some .h files? I have already imported ReactiveCocoa.h. Is there something 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; } 
+5
source share
2 answers

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

Try adding this line (per @chakming comment):

#import "ReactiveCocoa/RACEXTScope.h"

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

#import <ReactiveCocoa/EXTScope.h>

+5
source

Not quite related to the question, but I thought I would comment and mention that your use and placement of weak and strong characters is incorrect. The correct version will 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 weakness once. Once defined, it is defined for the entire area.

Similarly, syntax is needed only once in a block. In addition, he needed the top of the block so that you canceled the weak link as a strong link as soon as possible.

By the time it was used in your example, it is possible that the captured weak reference to the "I" could go out of scope.

+1
source

Source: https://habr.com/ru/post/1200317/


All Articles