Swift:在 deinit 方法中没有 println 输出(不使用操场) [英] Swift: no output for println in deinit method (not using playground)

查看:50
本文介绍了Swift:在 deinit 方法中没有 println 输出(不使用操场)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试代码(在终端中运行):

This is my test code (run in the terminal):

#!/usr/bin/xcrun swift

var count = 0;  // for reference counting

class A {
    init() {
        count++;
    }
    deinit {
        println("A deinit")
        count--;
    }
}

var a: A? = A()
println(count)
a = nil  // no output if I comment out this statement
println(count)

输出:

1
A deinit
0

如果上面提到的行被注释掉,则没有输出"A deinit".输出将是:

There is no output "A deinit" if the line mentioned above is commented out. And the output will be:

1
1

我使用了 swiftc 来编译代码,但结果仍然相同.( xcrun swiftc -o test test.swift )

I've used swiftc to compile the code but the result is still the same. (xcrun swiftc -o test test.swift)

是设计使stdout在程序退出时关闭,还是在销毁对象时仍然引用对象(由什么?)?

Is it by design that the stdout will be closed when the program exits, or the objects are still referred (by what?) when they are destructed?

在函数内部运行时,即使我注释掉 a = nil :

When it is run inside a function, it will output A deinit even if I comment out a = nil:

#!/usr/bin/xcrun swift

class A {
    deinit {
        println("A deinit")
    }
}

func test() {
    var a: A? = A()
    //a = nil
}

test()

我不在Xcode中使用游乐场.:-$

#!/usr/bin/xcrun swift

import Foundation

class A {
    deinit {
        var s = "A deinit"
        println(s)

        var a: A? = A()
        a = nil

        var error: NSError?
        var path = "\(NSFileManager.defaultManager().currentDirectoryPath)/swift_test.txt"
        if s.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: &error) {
            println("File saved at \(path)")
        } else {
            println(error)
        }
    }
}

//func test() {
    var a: A? = A()
//}
//test()

结果:除非在 test 函数中运行,否则不会输出到stdout或文件.

Result: No output to stdout or the file, unless running in the test function.

推荐答案

虽然我没有看到明确的参考,说"Swift的 deinit 的语义与ObjC的 dealloc ,"很难想象这不是真的,因为Swift和ObjC对象都由ARC管理,并且通常来说是可互换的.

While I don't see an explicit reference that says "Swift's deinit has exactly the same semantics as ObjC's dealloc," it is hard to imagine that this isn't true, since Swift and ObjC objects are both managed by ARC and generally speaking interchangeable.

鉴于此,这是完全可以预期的.可可不会在程序终止时取消分配对象.它只是终止,泄漏所有内存,文件句柄和其他系统资源,然后将其留给OS进行清理.这使得程序终止比以前快得多.

Given that, this is exactly expected. Cocoa does not deallocate objects at program termination. It just terminates, leaks all the memory, file handles, and other system resources, and leaves it to the OS to cleanup. This makes program termination dramatically faster than it would be otherwise.

这很重要,因为这意味着您通常不应该使用 deinit 来管理除操作系统管理的资源以外的任何东西(肯定不是您需要运行的任何东西)..当然,即使在C ++中,也没有办法保证运行析构函数.如果崩溃,那将不会发生,并且您的程序必须对此进行处理.您可以将其想象为所有可可程序在终止时都会安静地崩溃.

This is an important point because it means you generally should not use deinit to manage anything other than OS-managed resources (certainly not anything that you require to run). Of course, there's no way to guarantee that a destructor runs, even in C++. If you crash, it won't happen, and your program would have to deal with that. You can think of it as all Cocoa programs quietly crashing when they terminate.

因此,在您的情况下, a = nil 导致 deinit 运行,而程序终止没有运行.

So in your case, a = nil is causing deinit to be run, while program termination does not.

这篇关于Swift:在 deinit 方法中没有 println 输出(不使用操场)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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