简体   繁体   English

Xcode 10 Beta 6 Playground中未调用Deinit方法

[英]Deinit method is not called in Xcode 10 Beta 6 playground

I was exploring memory management concept and found deinit method is not calling in Xcode 10 beta 6 playground . 我正在研究内存管理概念,发现deinit方法未在Xcode 10 beta 6操场上调用。 Initially I thought may be some problem in code. 最初,我认为可能是代码中的一些问题。 Then I test same code in Xcode 9.4.1 playground and Xcode 10 beta 6 sample app everything is working as expected (deinit method is called). 然后,我在Xcode 9.4.1 Playground和Xcode 10 beta 6示例应用程序中测试了相同的代码,一切都按预期工作(调用了deinit方法)。 Is it bug in Xcode 10 beta 6 playground or anything else? Xcode 10 beta 6游乐场中的错误还是其他错误? I am using apple developer's code- 我正在使用苹果开发人员的代码-

class Person {
    let name: String
    weak var apartment: Apartment?

    init(name: String) {
        self.name = name
        print("\(name) is being initialized")
    }

    deinit { print("\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    weak var tenant: Person?

    init(unit: String) { self.unit = unit
        print("Apartment \(unit) is being initialized")
    }
    deinit { print("Apartment \(unit) is being deinitialized") }
}

do {
    var john: Person?
    var unit4A: Apartment?
    john = Person(name: "John Appleseed")
    unit4A = Apartment(unit: "4A")

    john!.apartment = unit4A
    unit4A!.tenant = john
    john = nil 
    unit4A = nil
}

This issue seems to still be present in Xcode 10.0 (10A255). Xcode 10.0(10A255)中似乎仍然存在此问题。 I have playground with the following code: 我的操场上有以下代码:

class Person {
    var name: String

    func buy(car: Car) {
        car.owner = self
    }

    init(name: String) {
        self.name = name
        print("Person \(name) is initalized")
    }

    deinit {
        print("Person \(name) is being deinitialized")
    }
}


class Car {
    let model: String
    weak var owner: Person?

    init(model: String) {
        self.model = model
        print("Car \(model) is initialized")
    }

    deinit {
        print("Car \(model) is being deinitialized")
    }
}

print("===== before do =====")
do {
    print("    ---- do begin -----")
    let johnny = Person(name: "John")
    let porsche = Car(model: "Carrera4")
    johnny.buy(car: porsche)
    print("    ---- do end -----")
}
print("===== after do =====")

In Xcode 9.4.1 both Car's and Person's deinit are executed, but in Xcode 10.0 (10A255) the Person's deinit method is not executed. 在Xcode 9.4.1中,将执行Car和Person的deinit方法,但是在Xcode 10.0(10A255)中,不会执行Person的deinit方法。

The same code inside a test macOS project works as expected (both deinit executed) in Xcode 9.4.1 as well as Xcode 10.0 (10A255) 测试macOS项目中的相同代码可以在Xcode 9.4.1和Xcode 10.0(10A255)中按预期方式工作(都执行deinit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM