【问题标题】:is it possible to deallocate a static variable manually in swift?是否可以在 swift 中手动释放静态变量?
【发布时间】:2016-10-27 10:45:36
【问题描述】:

我只需要在应用启动时存储一个静态变量,而且是很短的时间。

我想在使用结束后释放该静态变量。

【问题讨论】:

  • 将其设为可选,完成后将其设置为nil
  • 是的,我可以做到零,我没想到。????谢谢顺便说一句!

标签: swift memory


【解决方案1】:

是的,静态变量可以用var 声明并用? 使其成为可选变量,就像实例和局部变量一样。

class Y { }

class X {
    static weak var myOptionalStaticThing:Y? = Y()

    func foo() {
        // X.myOptionalStaticThing gets deallocated after this
        // if this were the only strong pointer to X.myOptionalStaticThing
        X.myOptionalStaticThing = nil
    }
}

【讨论】:

    猜你喜欢
    • 2018-11-14
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2011-09-27
    • 2011-11-11
    相关资源
    最近更新 更多