@selector() in Swift?

后端 未结 23 2592
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
23条回答
  •  Happy的楠姐
    2020-11-21 16:17

    Just in case somebody else have the same problem I had with NSTimer where none of the other answers fixed the issue, is really important to mention that, if you are using a class that do not inherits from NSObject either directly or deep in the hierarchy(e.g. manually created swift files), none of the other answers will work even when is specified as follows:

    let timer = NSTimer(timeInterval: 1, target: self, selector: "test", 
                        userInfo: nil, repeats: false)
    func test () {}
    

    Without changing anything else other than just making the class inherit from NSObject I stopped getting the "Unrecognized selector" Error and got my logic working as expected.

提交回复
热议问题