1

Hi I am using this code to try and animate a progress bar based on time.

  import UIKit

class LoadingScreen: UIViewController {

    @IBOutlet var progressView: UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()



        var time = 0.0
        var timer: NSTimer

        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)

        func setProgress() {
            time += 0.1
                progressView.progress = time / 3
            if time >= 3 {
                timer.invalidate()
            }
        }

However I get an error which says: cannot sign a value of type double to a value of type float.

EDIT:

The error is on this line:

progressView.progress = time / 3 
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
dwinnbrown
  • 3,789
  • 9
  • 35
  • 60

1 Answers1

7

Unless told otherwise swift compiler assumes type inference of Double for 0.0 - declare as

var time : Float = 0.0

Reference - https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html - Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.

Dave Roberts
  • 672
  • 3
  • 9
  • This worked to remove the error but now it crashes with the dreaded SIGABRT. The debug console shows: unrecognized selector sent to instance 0x7feef0708450' – dwinnbrown Jul 15 '15 at 14:35
  • 3
    this is a different problem - my best guess is you have declared your function in viewDidLoad instead of in the class - so as the console suggests, there is no selector setProgress in your class – Dave Roberts Jul 15 '15 at 14:39
  • 1
    The reason it crashes is that the code runs since @DaveRoberts fixed your problem. I would recommend you to accept his answer, and if you can't solve the new problem after debugging posting a *new* question about it. – Anders Jul 15 '15 at 15:29