Weak life cycle

I wrote a small class extension for UITableViewRowAction, while doing my Unit Test I've notice that the instance created with the extension 'init' method were alive longuer that I would have expected.


So I've trim down the implementation to the bare minimum to test that difference.


public extension  UITableViewRowAction  {
    public convenience init(title: String!, style: UITableViewRowActionStyle, exposedHandler: (UITableViewRowAction!, NSIndexPath!) -> Void) {
        self.init(style: style, title: title, handler: exposedHandler)
    }
}
class UITableViewRowAction_test: XCTestCase {

    func test_deallocationOfStoredItems() {
     
        weak var a1 : UITableViewRowAction?
        weak var a2 : UITableViewRowAction?
        if true {
            var action : UITableViewRowAction?
            action = UITableViewRowAction(title: "title", style: .Normal) { (a: UITableViewRowAction!, b: NSIndexPath!) -> Void in }
            a1 = action
         
            action = UITableViewRowAction(style: .Normal, title: "title", handler: { (a: UITableViewRowAction!, b: NSIndexPath!) -> Void in })
            a2 = action
            println("here a2 and a1 are still alive")
        }
        println("Here a2 == nil, but a1 is still alive")
        println("I would expect both to behave the same")
        XCTAssertTrue(a1 == nil, "This is failing")
        XCTAssertTrue(a2 == nil, "This is passing")
    }
}


Can someone explain to me why the 2 assert don't gave the same result?


---------------------------------------------------

Addition


If I reverse the a1 and a2 creation the result are reverse as well.

It look like the first instance created in that 'if true' will live longuer

I have the same problem if I create only 1 elements, it is still not nil when it get out of scope.


public extension  UITableViewRowAction  {
    public convenience init(title: String!, style: UITableViewRowActionStyle, exposedHandler: (UITableViewRowAction!, NSIndexPath!) -> Void) {
        self.init(style: style, title: title, handler: exposedHandler)
    }
}
class UITableViewRowAction_test: XCTestCase {
   
    func test_deallocationOfStoredItems() {
       
        weak var a1 : UITableViewRowAction?
        weak var a2 : UITableViewRowAction?
        if true {
            var action : UITableViewRowAction?
            action = UITableViewRowAction(style: .Normal, title: "title", handler: { (a: UITableViewRowAction!, b: NSIndexPath!) -> Void in })
            a2 = action
            action = UITableViewRowAction(title: "title", style: .Normal) { (a: UITableViewRowAction!, b: NSIndexPath!) -> Void in }
            a1 = action
           
        }
        XCTAssertTrue(a1 == nil, "This is now passing")
        XCTAssertTrue(a2 == nil, "This is now failling")
    }
}
Weak life cycle
 
 
Q