4

I made a simple count down app and I get this wrong message: cannot call value of non-function type "calendar".

What I need to change?

    @IBOutlet weak var deteLabelOutlet: UILabel!
    let formatter = DateFormatter()
    let useCalendar = NSCalendar.current()
    let requesedComponent: NSCalendar.Unit = [
        NSCalendar.Unit.month,
        NSCalendar.Unit.day,
        NSCalendar.Unit.hour,
        NSCalendar.Unit.minute,
        NSCalendar.Unit.second
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true)
        timer.fire()
    }

    func printTime() {
        formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
        let startTime = Date()
        let endTime = formatter.date(from: String("12/25/16 12:00:00 a"))

        let timeDiffrenece = useCalendar.components(requesedComponent,fromDate: startTime, toDate: endTime!, options: [] )

        deteLabelOutlet.text = "\(timeDiffrenece.month)Month \(timeDiffrenece.day) days \(timeDiffrenece.minute) minutes \(timeDiffrenece.second) seconds"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34

1 Answers1

1

In Swift 3 the current calendar is not a function. Remove the parentheses.

Anyway it's highly recommended to use the native struct Calendar as suggested in the other answer

let useCalendar = Calendar.current

Edit:

You have to unwrap all date components when assigning the values to the label because in Swift 3 all date components are optional. However as all relevant components are specified you can forced unwrap the components safely.

deteLabelOutlet.text = "\(timeDiffrenece.month!)Month \(timeDiffrenece.day!) days \(timeDiffrenece.minute!) minutes \(timeDiffrenece.second!) seconds"
vadian
  • 274,689
  • 30
  • 353
  • 361