Swift: unclear weak/strong behavior with captured variables in blocks

Originator:raphael
Number:rdar://17533241 Date Originated:02-Jul-2014 10:50 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 6 Beta 2
Classification:Enhancement Reproducible:Not Applicable
 
Swift defines a mechanism to avoid retain cycle in blocks (that are affected to property of self)

var myClosure = {
    [weak obj] in
    println(obj?.description)
}

This is not clear whether self is constant within the block or not. In Obj-C, 2 possible approach are possible:

Approach A:
__weak id obj;
^{
	NSLog(obj);
	NSLog(obj);
}

OR

Approach B:
__weak id obj;
^{
	__strong id sobj = obj;
	NSLog(sobj);
	NSLog(sobj);
}

The first approach makes it so that obj can become nil at any point within the block, while approach B makes it either nil or not nil but non-changing  within the block.

In Swift, it's not clear whether Approach A or B applies when using the [weak obj] pattern.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!