Unrecognized selector sent to instance NSTimer Swift

后端 未结 3 1662
轮回少年
轮回少年 2021-01-05 08:35

I\'m trying to developing an app that includes a simple Stopwatch feature. I\'m using Xcode 6 and the Swift language. Here is the code in FirstViewController



        
相关标签:
3条回答
  • 2021-01-05 09:08

    You have defined the updateTime as a local function to Stopwatch, so it is not available outside of its scope. You should move it at class level (same as Stopwatch) in order to be accessible.

    I notice however that there are some local variables which should probably be instance properties.

    0 讨论(0)
  • 2021-01-05 09:11

    let updateTime be a class method .
    And if it's in a pure Swift class you'll need to preface that method's declaration with @objc like:

    class A {
         @objc func updateTime(timer:NSTimer) {
    
         }
    }
    
    0 讨论(0)
  • 2021-01-05 09:18

    Another possibility: make sure your selector function doesn't throw, as that will also give you an unrecognized selector exception:

    func mySelector(timer: NSTimer) throws {
    }
    
    0 讨论(0)
提交回复
热议问题