An error of swift language guide 4.2 of Automatic Reference Counting

https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html ,


class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
    deinit { print("\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
    weak var tenant: Person?
    deinit { print("Apartment \(unit) is being deinitialized") }
}

var john: Person?
var unit4A: Apartment?

john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")

john?.apartment = unit4A
unit4A?.tenant = john // I change '!' to '?', it works, but why

john = nil
unit4A = nil

This two object can't realease when I use !,xcode10 beta,playground

The example in the guide is correct; this looks like a behavior change in playgrounds. I ran the example (with forced unwraps) in the following scenarios and always saw the deinitialization prints, except in the situation you described:

  • :white_check_mark: Command-Line Tool project, Xcode 10 beta 2 (10L177m).
  • :white_check_mark: xcrun swift REPL from Xcode 10 beta 2.
  • :white_check_mark: swiftc from Xcode 10 beta 2.
  • :white_check_mark: Playground in Xcode 9.4.
  • :x: Playground in Xcode 10 beta 2.

I'd recommend filing a bug for this.

1 Like