UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: BetterRest confusion as binary operator used with two different types without conversion

Forums > 100 Days of SwiftUI

So I have just finished the second episode on day 27 for the BetterRest app and in it Paul subtracts a Double from a Date.

How is this possible? as you can't subtract an Int from a Double because they're two different types.

So how are you able to subtract a Double from a Date?

2      

As Paul says in the video and the notes, Apple have provided the API. This means that they have defined the operation of subtracting a TimeInterval (which is a Double) directly from a Date.

If you open the Quick Help in Xcode ( ⌥ + ⌘+ 3 )and search for Date, you will see that there is a static func - defined for Date and TimeInterval.

3      

Thanks. It was just confusing that you can't mix types. It still doesn't make sense two different types being used with the operator but now I know it's possible and TimeInterval is just a typealias for Double

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.