在SwiftARC 中,deinit当 anyUIViewController从内存中删除时正在调用,但如果UIView从内存中删除任何内容,则不会调用它。
例如
如果UIViewControllerClassdeinit工作得很好
class MusicPlayerUIViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
{
deinit
{
APP_DELEGATE.RemovePlayerContents()
}
}
Run Code Online (Sandbox Code Playgroud)
但如果UIView班级deinit不工作
class MusicPlayerView: UIView,UITableViewDelegate,UITableViewDataSource
{
deinit
{
APP_DELEGATE.RemovePlayerContents()
}
}
Run Code Online (Sandbox Code Playgroud)
任何的想法 。
我想在我的项目中 UIViewController 的每个子类中的 deinit 中记录一些语句。我不想在每个视图控制器子类中复制/粘贴相同的行。
在Swift中(我使用的是4.1),当一个对象被破坏时,有没有办法在扩展中做一些清理工作?我想到了可以进入的代码类型deinit(),但扩展无法声明deinit().(除此之外,如果需要多个扩展,则会有多个deinit()声明.)
从 ios 9 开始,没有必要取消订阅通知中心,因为 ios 会自动处理这一点,但在 ios 9 之前,开发人员必须手动调用NotificationCenter.default.removeObserver(self)以避免内存泄漏,这是常见的地方(在很多教程中都建议和 Stackoverflow 帖子)deinit。所以,我的问题是 - 如何deinit从通知中取消注册,因为deinit只在对象释放之前调用,所以它的引用计数应该是 0,但肯定不是 - 因为我们仍然订阅通知中心。实现这一点的唯一可能方法似乎是使用弱引用,基本上如果通知中心弱引用对象,上述场景是可能的,但在这种情况下,根本不需要取消订阅,因为对象可以很容易地被释放。有人可以澄清一下这是如何工作的。
先发生哪一个?
deinit我正在使用以下代码在类的init上创建NSURLSession:
dispatch_once(&Static.token) { [unowned self] in
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(CONSTANTS.BackgroundSessionUploadIdentifier)
Static.session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}
Run Code Online (Sandbox Code Playgroud)
完成后,类没有被删除,任何未来上传的'回调都将第一个类实例化作为自己.上传完成后,如何正确清理NSURLSession?
我的全班都可以在这里看到:
我有一个汽车课.让我们说一辆车去垃圾场,这辆车应该不再算在总人口中了.我有deinit功能,但我如何系统地从汽车人口中删除汽车?换句话说,我如何让deinit生效?
我有一个类变量,isJunk但不知道如何使用它来使这项工作.
class Car {
static var population: Int = 0
var isJunk: Bool = false
var color: String
var capacity: Int
var driver: Bool?
var carOn: Bool = false
init (carColor: String, carCapacity: Int) {
self.capacity = carCapacity
self.color = carColor
Car.population += 1
}
deinit {
Car.population -= 1
}
func startCar() {
self.carOn = true
}
}
Run Code Online (Sandbox Code Playgroud) 从另一个类创建子类时,override该init()函数需要它,但您不能覆盖deinit"函数".
这在Swift中可能吗?
这是一个例子
class Foo {
init(){
print("Foo created")
}
deinit {
print("Foo gone")
}
}
class Bar: Foo {
override init(){
print("Bar created")
}
//Is not overwritten here
deinit {
print("Bar gone")
}
}
Run Code Online (Sandbox Code Playgroud)
里面的示例viewcontroller
override func viewDidLoad() {
super.viewDidLoad()
var f: Foo?
f = Foo()
f = Bar()
f = nil
}
Run Code Online (Sandbox Code Playgroud)
产量
Foo created //Foo object initialised - Foo init() called
Foo created //Foo init() called before calling Bar init()? no …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能在我的视图控制器中使用惰性属性,并在deinit方法中仅在初始化时才调用我的惰性属性的方法。下面是一些代码:
fileprivate lazy var session: MySession = {
let session: MySession = MySession()
session.delegate = self
return session
}()
deinit {
session.delete()
}
Run Code Online (Sandbox Code Playgroud)
这样,当方法session.delete()中deinit被调用且session尚未使用时(仍然如此nil),它会被初始化,然后delete被调用。我不想要这个。我只想在之前已初始化的delete情况下调用。session
有办法实现这一点吗?我必须放弃惰性初始化的想法吗?
我想知道在每个视图控制器上实现 adeinit以检查它消失时是否被正确删除并避免内存泄漏是否是一个好习惯?
使用NSTimer之后,将永远不会调用类deinit方法。该类是用Swift编写的,我在“ init”处调用计时器,如下所示:
init(eventsDistributor : EventsDistributor, orderSide : ORDER_SIDE, stockViewModel : StockViewModel, orderType : ORDER_TYPE_ENUM, orderPrice : NSNumber) {
self.eventsDistributor = eventsDistributor
self.orderSide = orderSide
self.stockViewModel = stockViewModel
self.orderType = orderType
self.orderPrice = orderPrice
super.init()
_events()
loadPreOrderDetails()
//Here I call the Timer:
var reloadTimer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "loadSecurityTradeInfo", userInfo: nil, repeats: true)
reloadTimer.fire()
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将NSTimer用作局部变量,但在该处没有成功...有人遇到此问题吗?我可以在Swift中使用一个不会导致不取消分配类的计时器吗?谢谢
deinit ×11
swift ×11
ios ×5
memory-leaks ×1
nstimer ×1
nsurlsession ×1
swift3 ×1
swizzling ×1
uiview ×1