2

I recently downloaded Xcode 7 and Swift 2.0. In Swift 1.2 I set a timer with the following code.

let timer = NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: Selector("didTimeout"), userInfo: nil, repeats: false) 

I do not use the timer constant anywhere I just schedule a timer.

I was under the understanding that in order to do this I had to use the "let timer =" portion.

With Swift 2.0 I get the following warning.

Initialization of immutable value 'timer' was never used; consider replacing with assignment to '_' or removing it.

I've never seen the use of _ before so I just replaced the line above with the line below.

NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: Selector("didTimeout"), userInfo: nil, repeats: false) 

Has not using "let timer" or "var timer" always been ok in Swift or is this new in Swift 2.0?

1 Answer 1

6

The way you did it is totally fine. If you try it, it will work.
_ is something to reduce memory, CPU and storage usage if created things are not needed. I actually do not know why they made it this way.

By the way you do not need Selector("didTimeout"). You can simply do "didTimeout:".

Hope that helps :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.