This is an extremely simple piece of code, but I feel like there is a more elegant way to do this:
if timeOfLastUpdate == nil {
timeOfLastUpdate = currentTime
return
}
//"Infinitesimal" moment of time used to update the position of the orbiter
let dt:CFTimeInterval = currentTime - timeOfLastUpdate!
timeOfLastUpdate = currentTime
//Other code
I feel like there should be a more elegant way to do the following using optional chaining maybe. I don't like the fact that
a) I'm checking if the value is equal to nil instead of using some kind of optional chaining
b) The line timeOfLastUpdate = currentTime
is repeated twice.
Is there a better way to do this? Something more in line with Swift?
How about this:
If you need the code to return without running your "other code" in the case where "timeOfLastUpdate is nil, you might need to have your
timeOfLastUpdate = currentTime
assignment appear twice: