So I have this snippet here which involves a block:
NSArray<Class> *acceptableClasses = @[[DesktopEntity class]];
__block NSInteger insertIdx = row;
[info enumerateDraggingItemsWithOptions:0 forView:_tableView classes:acceptableClasses searchOptions:nil usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop)
{
DesktopEntity *entity = draggingItem.item;
[_tableContents insertObject:entity atIndex: insertIdx];
[_tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:insertIdx] withAnimation:NSTableViewAnimationEffectGap];
draggingItem.draggingFrame = [_tableView frameOfCellAtColumn:0 row: insertIdx];
++insertIdx;
}];
it compiles fine but I'm getting a warning:
Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior
I'm having some trouble understanding what it means. I managed to make the warning disappear by just typing the self keyword before the variables:
[self.tableContents insertObject:entity atIndex: insertIdx];
[self.tableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:insertIdx] withAnimation:NSTableViewAnimationEffectGap];
draggingItem.draggingFrame = [self.tableView frameOfCellAtColumn:0 row: insertIdx];
++insertIdx;
But I've also seen people using constructs such as
__weak typeof(self) weakSelf = self;
and then using this 'weakSelf' instead, what's the difference here and is my first approach wrong?
_tableContentsand_tableVieware instance variables though.@property NSMutableArray* tableContents;and@property (weak) IBOutlet NSTableView *tableView;am I missing something?self.tableContents, but_tableContentsis it's instance variable. It's just compiler declaring ivars automatically for you.