Swift: willSet and didSet are not called if optional properties become nil

Originator:stephan.michels
Number:rdar://17305023 Date Originated:13-Jun-2014 08:16 PM
Status:Open Resolved:
Product:Developer Tools Product Version:10.10
Classification:Other Bug Reproducible:Always
 
Summary:
I hoped that willSet and didSet will be called if optional properties become nil, because the object get deallocated.
But they are not called. We get the same problems like in Objective-C.

Steps to Reproduce:
import Foundation

class ObjectA {
	init() {
		println("Init of \(self)");
	}
	
	deinit {
		println("Deinit of \(self)");
	}
}

class ObjectB {
	weak var instanceA : ObjectA? {
		willSet(newValue) {
			println("Will set value to \(newValue)")
		}
		didSet {
			println("Did set value to \(instanceA)")
		}
	}
}

var instanceA : ObjectA?
var instanceB = ObjectB();

autoreleasepool {
	println("\nStep 1");
	instanceA = ObjectA();
	
	println("\nStep 2");
	instanceB.instanceA = instanceA
	println("value of ivar: \(instanceB.instanceA)");
	
	println("\nStep 3");
	instanceA = nil
	
	println("\nStep 4");
	println("value of ivar: \(instanceB.instanceA)");
}

println("Finish")

Actual Results:
I got following output:
Step 1
Init of C9TestSwift7ObjectA (has 0 children)

Step 2
Will set value to C9TestSwift7ObjectA (has 0 children)
Did set value to C9TestSwift7ObjectA (has 0 children)
value of ivar: C9TestSwift7ObjectA (has 0 children)

Step 3
Deinit of C9TestSwift7ObjectA (has 0 children)

Step 4
value of ivar: nil
Finish

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!