11

Currently i'm doing like this,

Calling selector as:

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "startAnimation:", userInfo: loadingView, repeats: true)

Selector method is as:

private class func startAnimation(timer:NSTimer){
    var loadingCircularView = timer.userInfo as UIView
}

I'm getting warning, and app crashes:

warning: object 0x67c98 of class ‘ClassName’ does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector +[ClassName startAnimation:]
3
  • Private method's are only available from the class' objects, not from other objects. If you really want to keep this method private, then write a public wrapper-method which then internally calls your private method May 4, 2015 at 10:03
  • Removing private keyword didn't help. May 4, 2015 at 10:18
  • Add @objc to the start of the method definition (as well as making it non-private) - though I'm unsure if a class method could still be a target/selector. Also self in this instance, unless you're in a class method is an instance.
    – Rich
    May 4, 2015 at 10:27

4 Answers 4

4

1. You can write your function as follow :

@objc private class func startAnimation() {}

or

dynamic private class func startAnimation() {}  // not recommended

When you declare a swift function as dynamic, you made it seems like a Objective-C function (Objective-C follows Dynamic Dispatch), or we can say, the function is a Dynamic Dispatch Function right now, which can be called in Selector.

But in this case, we just need to let this function has a Dynamic Feature, so declare it as @objc is enough.

2. If you write your function as

@objc private func xxxxx(){}

the target in NSTimer should be self, but if you write your function as

@objc private class func xxxx(){} // Assuming your class name is 'MyClass'

the target in NSTimer should be MyClass.self.

2
  • Thanks for the clear answer. does @objc have some downsides to be mindful of when using? Oct 20, 2016 at 7:15
  • 1
    @dhfromkorea I suppose not. Actually, when you want to declare a private func in a viewcontroller, but not a internal func, (at)objc would be necessary.
    – Kaiyuan Xu
    Oct 20, 2016 at 7:44
4

If you're fine with changing the class function to be an instance function you could use this:

performSelector(Selector(extendedGraphemeClusterLiteral: "aVeryPrivateFunction"))

Note: be sure to mark your private function with @objc like this:

@objc private func aVeryPrivateFunction(){
    print("I was just accessed from outside")
}

read more here

3

You can't call private methods with selectors. That is the whole point of making the methods private, so they are not accessible from the outside.

You are also sending an instance of self as target to class method which is why it will not work. You need to either send a class or remove class from method.

3
1

Adding NSObject when class declaration solved my problem.

Ref:NSTimer scheduledTimerWithTimeInterval and target is "class level function"

class MyClass:NSObject{}

and calling method as,

NSTimer.scheduledTimerWithTimeInterval(0.5, target: ClassName.self, selector: Selector("startAnimation"), userInfo: nil, repeats: true)

class func startAnimation(){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.