Extra argument 'selector' in call error - ios

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.

Related

Scheduled timers in SpriteKit

I have scheduled timers that add sprite nodes to the screen as obstacles
func timers(){
personTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(spawnPerson), userInfo: nil, repeats: true)
bikeTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(spawnBike), userInfo: nil, repeats: true)
motorcycleTimer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(spawnMotorcycle), userInfo: nil, repeats: true)
}
I added a function to invalidate those timers. so that a bonus "level" can be ran.
func invalidateTimers(){
// Obstacles
personTimer.invalidate()
bikeTimer.invalidate()
motorcycleTimer.invalidate()
}
When the bonus is called
func bonus() {
invalidateTimers()
bonusTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(spawnDiamonds), userInfo: nil, repeats: true)
}
The problem that I'm having is that when the bonus is done running I invalidate the bonusTimer and recall timers(). But when I do all the timers in the function seem to be firing twice. Whats an easy workaround for that since they can't just be paused.
Instead of using timers, consider using SKActions, as they work well with SpriteKit. To start the timer, run:
let wait1 = SKAction.wait(forDuration: 1)
let personTimer = SKAction.repeatForever(SKAction.sequence([wait1, SKAction.run {
spawnPerson() // spawnBike() etc. for each different timer
}]))
self.run(personTimer, withKey: "spawnPerson")
with modified wait values and function calls for each different timer. Then to stop the timer, run:
self.removeAction(forKey: "spawnPerson")
for each action using a different key.
Instead of using timers, you can use update: method of your SKScene subclass. It calls once per frame and has currentTime parameter. So you can easily calculate time intervals you need and trigger corresponding methods.

Swift: prevent NSTimer starting automatically

I am trying to use the NSTimer to increment the progress bar in my app when recording voice (see the screenshot)
let timedClock = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting:"), userInfo: nil, repeats: true)
internal func Counting(timer: NSTimer!) {
if timeCount == 0 {
//self.timedClock = nil
stopRecording(self) //performs segue to another view controller
} else {
timeCount--;
self.timer.text = "\(timeCount)"
}
print("counting called!")
progressBar.progress += 0.2
}
The progress bar works only for the first time after I compile and run the project. When the recording is finished, the app performs segue to another view controller to play the recorded audio. However, when I go back to the view for recording, the timer/progress bar automatically runs. I suspect the NSTimer object is still alive on the NSRunLoop. So I was wondering how to prevent the NSTimer from automatically running.
Inspired by the answer in this SO thread, I tried the following, but the NSTimer still automatically runs.
let timedClock = NSTimer(timeInterval: 1, target: self, selector: "Counting:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timedClock, forMode: NSRunLoopCommonModes)
This happens because when your controller created it's properties are automatically initialized. According to Apple Docs (and method's name) scheduledTimerWithTimeInterval create and return scheduled timer. So if you only want create your timer and call it by trigger function use it like this:
class MyClass {
var timer: NSTimer?
...
func enableTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting:"), userInfo: nil, repeats: true)
}
func disableTimer() {
timer?.invalidate()
timer = nil
}
...
}
Sorry for the quick self-answer, as I just found out that I can use the invalidate() method to prevent the timer from automatically firing:
timedClock.invalidate()
Hope it helps someone in the future!

How to add a timer to a function - IOS Swift

Say I had a function, any function, that I wanted to run for only three seconds and then never run again. How would I do this? Would I use NSTimer? Thank you for your help.
Here you go,
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("updateProgress:"), userInfo: nil, repeats: false)
func updateProgress(timer: NSTimer){
}

Define a variable as AnyObject so that it can be changed later to NSTimer--Swift

So, I am trying to make a timer (run correctly) in swift.
func doSomething(){
println("Did something")
}
#IBActionFunc createTimer: AnyObject{
var timer = NSTimer(timeInterval: 0.2, target: self, selector: "doSomething", userInfo: nil, repeats: true)
}
#IBActionFunc stopTimer: AnyObject{
timer.invalidate()
}
Other option:
var timer:AnyObject = AnyObject
func doSomething(){
println("Did something")
}
#IBActionFunc createTimer: AnyObject{
timer = NSTimer(timeInterval: 0.2, target: self, selector: "doSomething", userInfo: nil, repeats: true)
}
#IBActionFunc stopTimer: AnyObject{
timer.invalidate()
}
I am not sure if this should actually work. From my tests, it does not because "timer" is defined as a local variable (?), so it cannot be accessed from other functions (?). To try to fix this, I first defined "var timer" to AnyObject, so that it can be redefined as anything later. Sadly, I get crazy errors and no's. Should I even need to do this second thing, or should the first one work? Thanks in advance!
You're right in that you currently have a local variable, so you can't reference it in other functions. You need an instance variable, such as:
class MyClass {
var timer: NSTimer
...
#IBActionFunc createTimer: AnyObject{
timer = NSTimer(timeInterval: 0.2, target: self, selector: "doSomething", userInfo: nil, repeats: true)
}
...

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