There are a few Objective-C versions of Sunrise/Sunset calculations floating out there, but nothing in pure Swift.
(I wrote this like 5 years ago, now there are...)
Maybe that has something to do with the Swift compiler not being keen on complex expressions.
I worked around this by breaking the expression into smaller steps, which was lots of fun.