在Xcode 10 Beta 6 Playground中未调用Deinit方法 [英] Deinit method is not called in Xcode 10 Beta 6 playground

查看:99
本文介绍了在Xcode 10 Beta 6 Playground中未调用Deinit方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索内存管理概念并发现 deinit方法未在 Xcode 10 beta 6游乐场中调用.最初,我认为可能是代码中的一些问题.然后,我在Xcode 9.4.1游乐场和Xcode 10 beta 6示例应用程序中测试了相同的代码,一切都按预期工作(调用了deinit方法).是 Xcode 10 beta 6游乐场中的错误还是其他错误?我正在使用Apple开发人员的代码-

I was exploring memory management concept and found deinit method is not calling in Xcode 10 beta 6 playground. 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). Is it bug in Xcode 10 beta 6 playground or anything else? 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
}

推荐答案

此问题似乎仍然存在于Xcode 10.0(10A255)中.我的游乐场带有以下代码:

This issue seems to still be present in 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 =====")

在Xcode 9.4.1中,将执行Car和Person的deinit方法,但是在Xcode 10.0(10A255)中,不会执行Person的deinit方法.

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.

测试macOS项目中的相同代码可以在Xcode 9.4.1和Xcode 10.0(10A255)中按预期工作(均执行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)

这篇关于在Xcode 10 Beta 6 Playground中未调用Deinit方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆