Swift Tips

How to check if two date ranges overlap in Swift

Published on: January 17, 2020

Dates in Swift can be compared to each other. This allows you to check whether one date comes before or after another date: if dateOne > dateTwo { print("dateOne comes after dateTwo") } else if dateOne < dateTwo { print("dateOne comes before dateTwo") } else if dateOne == dateTwo { print("dateOne is equal to dateTwo") } You can also use dates in Swift to create ranges. And when you have one range, you can check whether it overlaps with another date. Let's look at an example: struct Meeting { let startDate: Date let endDate: Date } func doMeetingsOverlap(_ meetingOne: Meeting,...

Read more...

Generics in Swift explained

Published on: December 5, 2019

Whenever we write code, we want our code to be well-designed. We want it to be flexible, elegant and safe. We want to make sure that Swift’s type system and the compiler catch as many of our mistakes as possible. It’s especially interesting how Swift’s type system can help us avoid obvious errors. For example, Swift won’t allow you to assign an Int to a String property like this: var anInt = 10 anInt = "Hello, World!" The Swift compiler would show you an error that explains that you can’t assign a String to an Int and you’d understand this....

Read more...