To avoid a memory leak when using NSBlockOperation in Objective-C, we would have to declare the variable as weak to be able to reference the block operation inside the block (to cancel if needed), typically like this:
__weak NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
if (blockOp.cancelled) {
...
}
}];
But in Swift, when I try declare my NSBlockOpeartion as weak, it is always nil.
weak var blockOp = NSBlockOperation()
Without the weak reference, all is fine except it is leaking a little bit of memory each time. How can I reference the block inside the block without leaking memory in Swift?