Swift selector - unrecognized selector sent to instance - ios

I have a function:
func runRockRotation(rockSprite: SKSpriteNode){
startRockRotationAnimation(rockSprite, isRock: true)
}
When I call it like this:
runRockRotation(rock)
it works, but I can't seem to be able to put it inside a NSTimer selector.
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation:", userInfo: rock, repeats: false)
Read a lot of forums, tried this:
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation()", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation(_)", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation", userInfo: rock, repeats: false)
Also, tried without rock, using nil, but nothing seems to work.
Every time I get:
2015-04-10 15:49:03.830 Meh[1640:218030] -[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840
2015-04-10 15:49:03.832 Meh[1640:218030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840'
How do I call my function in a selector with a parameter? I know how to do that in Objective C, but can't seem to do it in Swift. All help will be appreciated.

Check the documentation for scheduledTimerWithTimeInterval
You will see that
The selector should have the following signature: timerFireMethod:
(including a colon to indicate that the method takes an argument).
The
timer passes itself as the argument, thus the method would adopt the
following pattern:
- (void)timerFireMethod:(NSTimer *)timer
Your function doesn't match this pattern, so the appropriate selector cannot be found.
Use something like -
func runRockRotationForTimer(_ timer: NSTimer){
self.runRockRotation(timer.userInfo as? SKSpriteNode)
timer.invalidate();
}
and schedule it using
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotationForTimer:", userInfo: rock, repeats: false)

Also helps to make sure the target object (self in your case) is a subclass of NSObject
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: <THIS MUST BE NSOBJECT SUBCLASS>, selector: "runRockRotation:", userInfo: rock, repeats: false)

Related

Swift 3.0: Timer not firing for target other than self

Timer.scheduledTimer(timeInterval: 5.0, target:self.notificationView, selector: #selector(NotificationView.self.timerFired(_:)), userInfo: nil, repeats: false)
func timerFired(_ timer: Timer) {
print("Timer Fired")
}
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue timerFired:]: unrecognized selector sent to instance 0x7fc0baf46f60'
I don't understand where is wrong? If target is self then everything works fine.
The problem is with your selector syntax it will be like this.
#selector(NotificationView.timerFired(_:))
Note : self is for current ViewController if you want to set action for another then you need to specify the class name.method in your case it is NotificationView.timerFired.
I try the following code and NotificationView.timerFired is triggered:
class NotificationView {
#objc func timerFired(_ timer: Timer) {
print("Timer Fired")
}
}
class ViewController: UIViewController {
let notificationView = NotificationView()
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(
timeInterval: 5.0,
target:self.notificationView,
selector: #selector(NotificationView.timerFired(_:)),
userInfo: nil,
repeats: false
)
}
}
Check this part of your error message:
[_SwiftValue timerFired:]
timerFired: is an Objective-C style notation of the selector. Seems your #selector(...) is working. (Though not recommended...)
_SwiftValue is a class name of the object being the target of the selector. This means your target target:self.notificationView is converted to _SwiftValue.
This may happen when you declare your notificationView as Optional or implicitly unwrapped Optional. If so, try this:
Timer.scheduledTimer(timeInterval: 5.0, target: self.notificationView!, selector: #selector(NotificationView.timerFired(_:)), userInfo: nil, repeats: false)
(Please do not miss the ! after self.notificationView.)
The code below worked for me (in playground/swift 3) :
class SomeClass {
#objc public func timerFired(_ timer: Timer) {
print("Timer Fired")
}
}
let s = SomeClass()
Timer.scheduledTimer(timeInterval: 5.0, target:s, selector: #selector(s.timerFired(_:)), userInfo: nil, repeats: false).fire()
//This also will work
//Timer.scheduledTimer(timeInterval: 5.0, target:s, selector: #selector(SomeClass.timerFired(_:)), userInfo: nil, repeats: false).fire()

NSTimer cancel my UIView.animateWithDuration

Every time NSTimer is called my animation is canceled
override func viewDidLoad() {
super.viewDidLoad()
_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer:", userInfo: nil, repeats: true)
self.mover()
}
/**
Selector
- parameter timer: NStimer
*/
func timer(timer:NSTimer!){
self.lblTiempo.text = "Te haz movido"
}
How can I prevent NSTimer cancel my animations?
/**
Mueve la imagen en la pantalla
*/
func mover(){
let x = Int(arc4random_uniform(UInt32( 400 )))
let y = Int(arc4random_uniform(UInt32( 400 )))
UIView.animateWithDuration(self.juego!.velocidad,
delay: 0.0,
options: [.CurveEaseInOut, .AllowUserInteraction],
animations: {
self.imgBicho.center = CGPoint(x: x, y: y)
},
completion: { finished in
self.mover()
})
}
I am relatively new to the development of IOS, someone can help me with this, thanks.
Sure paulvs, Sorry it is a misspelling. The problem is with the NSTimer :(
I think the code you posted will crash straight away with an exception like this
'NSInvalidArgumentException', reason: '-[TestTimer.ViewController timer]: unrecognized selector sent to instance 0x7fb501c3faa0'
because the timer function has one argument
func timer(timer:NSTimer!){
while your NSTimer initialisation code passes a selector with no arguments
_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer", userInfo: nil, repeats: true)
To fix this, change this line to
_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer:", userInfo: nil, repeats: true)
notice the colon : near the end.

Swift NSTimer unrecognized selector sent to instance timerFireMethod

I'm writing some timer code in Swift for iOS 9.2
I have the latest iOS 9.2 docs downloaded through xcode
They show
(void)timerFireMethod:(NSTimer *)timer
But this will not work.
If I use signatures like these
func timerFire(timer : NSTimer?)
func timerFire(timer : NSTimer)
Then I get the error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DSP1.PlayManager timerFire]: unrecognized selector sent to instance
The only thing I can get to work is a call signature like this
func play(sound : String)
{
bsound = theLM?.getPlayer(sound)
bsound?.delegate = self
bsound?.play()
stimer = NSTimer(timeInterval: 1.0, target: self, selector: Selector("timerFire"), userInfo: self, repeats: true)
NSRunLoop.currentRunLoop().addTimer(stimer!, forMode: NSDefaultRunLoopMode)
}
// Docs say signature should be (void)timerFireMethod:(NSTimer *)timer. Docs are wrong
func timerFire()
{
print("Player at: \(bsound?.currentTime) out of \(bsound?.duration) seconds");
}
But this does not match what the latest iOS 9.2 docs downloaded through XCode say should work.
Am I doing this right?
Why do the freshly loaded iOS 9.2 docs seem to have the wrong signature?
What are other people reading for accurate documentation for Swift iOS programming?
(Edited to be more clear that the callback signatures listed in the docs fail to work at runtime)
Answer:
In the call to NSTimer, if your function name passed in as Selector has a trailing colon, it means you want the timer passed as an argument to your method. No colon means you don't want the timer passed as an argument.
NSTimer(timeInterval: 1.0, target: self, selector: Selector("timerFire"), userInfo: self, repeats: true)
func timerFire()
OR
NSTimer(timeInterval: 1.0, target: self, selector: Selector("timerFire:"), userInfo: self, repeats: true)
func timerFire(timer : NSTimer)
The documentation for NSTimer mentions this for the selector argument, but is far from clear. "The selector should have the following signature: timerFireMethod: (including a colon to indicate that the method takes an argument). "
You've made a simple, yet common mistake.
Your method signature should be:
func timerFire(timer: NSTimer) {}
And your timer setup should be:
NSTimer(timeInterval: 1.0, target: self, selector: "timerFire:", userInfo: nil, repeats: true)
The mistake is that you're missing the colon in the selector name. timerFire is different from timerFire:. Skip the colon and it'll look for for a method like this:
func timerFire() {}
Without the NSTimer parameter. It's best though to include the parameter, and thus the colon, so that you can confirm the timer you get is the one you expect.
The same is true for notifications. If you're using Notification Center, include the colon, and the Notification object in the method.
Your code seems to be ok. The selector for your timer does not have a predefined signature, you can all it whatever you like as long a you have a method in your class with that name.
func play(sound : String) {
// ....
stimer = NSTimer(timeInterval: 1.0, target: self, selector: "methodToRunOnTimerTick", userInfo: self, repeats: true)
NSRunLoop.currentRunLoop().addTimer(stimer!, forMode: NSDefaultRunLoopMode)
}
func methodToRunOnTimerTick() {
print("Player at: \(bsound?.currentTime) out of \(bsound?.duration) seconds");
}
One more thing to remember is that the method you decide to use can also receive the timer as a parameter when it is being called. This case would look like this:
func play(sound : String) {
// ....
stimer = NSTimer(timeInterval: 1.0, target: self, selector: "methodToRunOnTimerTick:", userInfo: self, repeats: true)
NSRunLoop.currentRunLoop().addTimer(stimer!, forMode: NSDefaultRunLoopMode)
}
func methodToRunOnTimerTick(timer: NSTimer) {
print("Player at: \(bsound?.currentTime) out of \(bsound?.duration) seconds");
}
So you implement the timer the right way, the method does not need a special signature. Let me know if you need more help. Good luck with your project!

Extra argument 'selector' in call error

class ViewController: UIViewController {
func ChangePage()
{
NSLog("Hej")
}
var timers = NSTimer(NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
}
I get the following error from Xcode 6:
Extra Argument 'selector' in call
I've tried several configurations, does it have something to do with where in the code it's placed?
You might want to use:
var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changePage", userInfo: nil, repeats: true)
This returns a timer that is already added to the run loop and fires automatically.
To stop the timer to fire, you must invalidate it like this
timer.invalidate()
You should add timeInterval in the constructor like:
NSTimer(timeInterval: NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
And yes, it does matter where you put. The problem is, that timers is a property, and it is created before the initialization. So when it is created, self is not existing, but you refer to it, and that causes the problem.

Can an NSTimer take multiple selectors?

I'm trying to use an NSTimer in my app, and was wondering if it's possible to call two methods when the timer fires.
Here's the code:
gameTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector:
Selector("gameMovement" && "fireBullet"), userInfo: nil, repeats: true)
I'm getting an error saying there are two arguments in the Selector.
Nope. You would call just one method that delegates to all the things you want.
func someFunc() {
gameTimer = NSTimer.scheduledTimerWithTimeInterval(
0.01,
target: self,
selector: Selector("timerFired"),
userInfo: nil,
repeats: true
)
}
func timerFired() {
gameMovement()
fireBullet()
}
This is a more maintainable pattern anyway, as it's easier to see how your code flows.

Resources