Swift 4 Timer与NSException崩溃

Bra*_*amp 3 xcode timer nstimer swift

我一直在寻找一种在Swift 4中使用Timer的方法并查看了这篇文章.我在xcode中测试了我的代码,当计时器第一次打勾时(在这种情况下10秒后)应用程序崩溃并且我收到错误,尽管构建成功了.

2017-11-20 19:54:42.781502-0700 Rock Prodigy[3022:554505] -[_SwiftValue   tick]: unrecognized selector sent to instance 0x608000051520
2017-11-20 19:54:42.791278-0700 Rock Prodigy[3022:554505] ***  Terminating app due to uncaught exception 'NSInvalidArgumentException',   reason: '-[_SwiftValue tick]: unrecognized selector sent to instance  0x608000051520'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010360d1ab __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x0000000102ca2f41 objc_exception_throw + 48
2   CoreFoundation                      0x000000010368da34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x00000001035900a8 ___forwarding___ + 1432
4   CoreFoundation                      0x000000010358fa88 _CF_forwarding_prep_0 + 120
5   Foundation                          0x000000010270e1ee __NSFireTimer + 83
6   CoreFoundation                      0x000000010359d2a4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7   CoreFoundation                      0x000000010359cf62 __CFRunLoopDoTimer + 1026
8   CoreFoundation                      0x000000010359cb1a __CFRunLoopDoTimers + 266
9   CoreFoundation                      0x0000000103594534 __CFRunLoopRun + 2308
10  CoreFoundation                      0x00000001035939b9 CFRunLoopRunSpecific + 409
11  GraphicsServices                    0x00000001090e29c6 GSEventRunModal + 62
12  UIKit                               0x0000000103a885e8 UIApplicationMain + 159
13  Rock Prodigy                        0x000000010238b637 main + 55
14  libdyld.dylib                       0x0000000107aa1d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
Run Code Online (Sandbox Code Playgroud)

这是我的快速4代码

import UIKit

class GeneralFitness: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    var timer: Timer! = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(tick), userInfo: nil, repeats: true)

    @objc func tick() {
        print("tick")
    }


}
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请告诉我.

And*_*ini 6

您的计时器以错误的方式初始化,因为:

如果使用闭包来初始化属性,请记住在执行闭包时尚未初始化实例的其余部分.这意味着您无法从闭包中访问任何其他属性值,即使这些属性具有默认值.

此外:

您也不能使用隐式self属性,也不能调用任何实例的方法(参见此处)

因此,要解决您的问题,您应该将这些代码移到里面viewDidLoad.所以你可以试试这个:

import UIKit

class ViewController: UIViewController {
   weak var timer: Timer?

   override func viewDidLoad() {
      super.viewDidLoad()
      Timer.scheduledTimer(withTimeInterval: 10, repeats: true, block: { [weak self] timer in
         self?.timer = timer
         self?.tick()
      })
   }

   deinit { 
      self.timer?.invalidate() 
   }

   @objc func tick() {
      print("tick")
   }
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 6

正如Andrea所说,你应该实例化计时器viewDidLoad.另外该文件说:

选择器应具有以下签名:( timerFireMethod:包括冒号以指示该方法接受参数).

例如,不要忘记禁用此计时器viewDidDisappear.你不能invalidate进去,deinit因为重复计时器保持对其目标的强引用,并且deinit只要计时器正在运行就不会被调用.如果你将其删除viewDidDisappear,你可能想要创建计时器viewDidAppear.

因此,导致类似于:

class ViewController: UIViewController {

    weak var timer: Timer?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(tick(_:)), userInfo: nil, repeats: true)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        timer?.invalidate()
    }

    @objc func tick(_ timer: Timer) {
        print("tick")
    }

}
Run Code Online (Sandbox Code Playgroud)

或者你可以使用带有[weak self]模式的基于块的再现,并且计时器不会保持对视图控制器的强引用,在这种情况下你可以使用deinit:

class ViewController: UIViewController {

    var timer: Timer?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        timer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { [weak self] timer in  // the `[weak self] reference is only needed if you reference `self` in the closure
            print("tick")
        }
    }

    deinit {
        timer?.invalidate()
    }

}
Run Code Online (Sandbox Code Playgroud)