强引用问题 OC版
1、方法一 didMoveToParentViewController
override func viewDidLoad() {
super.viewDidLoad()
self.timerDyz = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerFire), userInfo: nil, repeats: true)
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
self.timerDyz?.invalidate()
self.timerDyz = nil
}
}
@objc func timerFire(){
print("timer fire")
}
deinit {
print("\(self) 走了")
}
2、方法二 block
override func viewDidLoad() {
super.viewDidLoad()
self.timerDyz = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
print("timer fire dyz")
})
}
deinit {
self.timerDyz?.invalidate()
self.timerDyz = nil
print("\(self) 走了")
}
3、方法三 引入中间变量
class ViewController: UIViewController {
var timerDyz: Timer?
let target = NSObject()
override func viewDidLoad() {
super.viewDidLoad()
class_addMethod(self.target.classForCoder, NSSelectorFromString("timerFiredyz"), method(for: NSSelectorFromString("timerFire")), "v@:")
timerDyz = Timer.init(timeInterval: 1, target: self.target, selector: NSSelectorFromString("timerFiredyz"), userInfo: nil, repeats: true)
RunLoop.current.add(timerDyz!, forMode: .common)
// let sel = NSSelectorFromString("timerFire")
// let method = class_getInstanceMethod(self.classForCoder, sel)
// class_addMethod(self.target.classForCoder, sel, method_getImplementation(method!), "v@:")
// timerDyz = Timer.init(timeInterval: 1, target: self.target, selector: NSSelectorFromString("timerFire"), userInfo: nil, repeats: true)
// RunLoop.current.add(timerDyz!, forMode: .common)
}
@objc func timerFire(){
print("timer fire")
}
deinit {
self.timerDyz?.invalidate()
self.timerDyz = nil
print("\(self) 走了")
}
}
3、方法三 引入中间层
class ViewController: UIViewController {
let proxy: LGProxy = LGProxy()
override func viewDidLoad() {
super.viewDidLoad()
let selector = NSSelectorFromString("timerFire")
proxy.lg_scheduledTimer(timeInterval: 1, target: self, selector: selector, userInfo: nil, repeats: true)
}
@objc func timerFire(){
print("timer fire")
}
deinit {
print("\(self) 走了")
}
}
class LGProxy: NSObject {
weak var target: NSObjectProtocol?
var sel: Selector?
var lgTimer: Timer? = nil
override init() {
super.init()
}
func lg_scheduledTimer(timeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool){
self.lgTimer = Timer.init(timeInterval: ti, target: self, selector: aSelector, userInfo: userInfo, repeats: yesOrNo)
RunLoop.current.add(self.lgTimer!, forMode: .common)
self.target = aTarget as? NSObjectProtocol
self.sel = aSelector
guard self.target?.responds(to: self.sel) == true else{
return
}
let method = class_getInstanceMethod(self.classForCoder, #selector(lgTimerFire))
class_replaceMethod(self.classForCoder, self.sel!, method_getImplementation(method!), method_getTypeEncoding(method!))
}
@objc fileprivate func lgTimerFire(){
if self.target != nil{
self.target!.perform(self.sel)
}
else{
self.lgTimer?.invalidate()
self.lgTimer = nil
}
}
override func forwardingTarget(for aSelector: Selector!) -> Any? {
if self.target?.responds(to: self.sel) == true {
return self.target
}
else{
print("心里没有点数,写这样的代码,还要我给你填坑")
return super.forwardingTarget(for: aSelector)
}
}
deinit {
print("\(self) 走了")
}
}
网友评论