NSTimer does not invoke a private func as selector

后端 未结 1 945
青春惊慌失措
青春惊慌失措 2020-12-11 07:59

I am working on a gist: PasteboardWatcher.swift in which I invoked a NSTimer object like this:

func startPolling () {
        // setup and start of timer
             


        
相关标签:
1条回答
  • 2020-12-11 08:21

    According to Using Swift with Cocoa and Objective-C:

    “ Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well.”

    Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 2 Prerelease).” iBooks. https://itun.es/us/utTW7.l

    So, you can mark your function with @objc to get the desired behavior. I just tested this in one of my apps where I had used public visibility because I assumed that Objective-C couldn't see private declarations at all, and it works when marked as private and decorated with @objc.


    I just saw this related question: Swift access control with target selectors — basically the same thing but I think yours is phrased in a more general way so not strictly a duplicate.

    0 讨论(0)
提交回复
热议问题