Mismatching types in time interval

Hello how can i unwrap and convert reaming time in good way in Swift3?

    var _date: Date?
    var _lastTimestamp: Date


    self._date = Date()
    let _interval: TimeInterval = self._lastTimestamp ? _date?.timeIntervalSince(_lastTimestamp!): 0


"Result values in '? :' expression have mismatching types 'TimeInterval?' and 'Int'

Replies

The immediate problem you’re having is that

self._date
is optional, so this construct:
_date?.timeIntervalSince(_lastTimestamp!)

produces an optional. My preferred way around that is to use a local non-optional reference and then assign it to

self._date
when I’m done.
let now = Date()
let _interval: TimeInterval = self._lastTimestamp ? now.timeIntervalSince(_lastTimestamp): 0.0
self._date = now

But that still won’t compile because the

self._lastTimestamp
to the left of the
?
is not a Boolean and I don’t even have a good guess as what you’re trying to do there.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"