I am trying to understand how escaping closures work in Swift 3? Coming from the Objective-C world, for scenarios where the closure could escape the return of its enclosing function you had to do something along these lines:
@property (nonatomic, copy/strong) void (^callback)(NSData *rawData);
-(BOOL)someFunctionThatConsumesABlock:(void (^)(NSData *rawData))block
{
if (callback) {
self.callback = block;
return YES;
}
return NO;
}
-(void)someFunctionThatExecutesAtSomeLaterPoint
{
NSData *data = [self bytesFromSomeProcess];
self.callback(data);
}
Basically in the above code in objective-C, the object consuming the block performs a heap copy and strongly retains the block. This strong retaining makes sense because the block needs to stick around beyond the scope of the function it is passed as an argument too (since its an escaping closure/block).
In Swift 3, it seems the above is no longer necessary. All one needs to do is just annotate the closure as "escaping." So what is Swift 3 doing under the hood? Is it doing the same thing as objective-C? In essence do escaping closures in Swift 3 get implicitly strongly retained by the object they are being passed too? I dont see how else the language could achieve this and have the closures "stick around."