Initializing Swift properties that require “self” as an argument

后端 未结 3 668
一向
一向 2020-12-11 02:45

I have a Swift class that I\'d like to look something like this:

class UpdateManager {
  let timer: NSTimer

  init() {
    timer = NSTimer(timeInterval: 600         


        
3条回答
  •  执笔经年
    2020-12-11 03:24

    You've found the primary use case of the Implicitly Unwrapped Optional.

    • You need to access self from init, before timer is initialized.
    • Otherwise, timer should never be nil, so you shouldn't have to check it outside of init.

    So, you should declare let timer: NSTimer!.

提交回复
热议问题