Recent articles

Jump to a random post

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...

Tips to ask better questions

Published on: January 15, 2020

As developers, we all get stuck sometimes. When this happens we start searching for solutions on Google, or we ask questions on Stackoverflow, on the Swift forums, the iOS Developers Slack community or other places. Over the past couple of years, I have been actively trying to help people solve problems they were stuck on and while doing that, I noticed that the art of asking good questions is not an easy one. In today's Quick tip I would like to offer you five tips that I think are important to help you get better at asking questions which will...

Read more...

Understanding Combine’s publishers and subscribers

Published on: January 13, 2020

In my previous post about Combine, I introduced you to Functional Reactive Programming (FRP) and I've shown you can subscribe to Combine publishers using the sink(receiveCompletion:receiveValue:) method. I also showed you how you can transform the output of publishers using some of its built-in functions like map and collect. This week I want to focus more on how Combine publishers and subscribers work under the hood. By the end of today's post, you should have a clear picture of what Combine's core components are, and how they relate to each other. The topics covered in today's post are the following:...

Read more...

Removing a specific object from an Array in Swift

Published on: January 10, 2020

Arrays in Swift are powerful and they come with many built-in capabilities. One of these capabilities is the ability to remove objects. If you want to remove a single, specific object from an Array and you know its index, you can use remove(at:) to delete that object: var array = [1, 2, 3] array.remove(at: 0) // array is now [2, 3] If you don't know the object's position in the Array and the Array's elements conform to Equatable, you can look up the first index of the object you're looking for with firstIndex(of:) and you can then proceed to delete...

Read more...

How to filter an Array in Swift?

Published on: January 9, 2020

When you have an Array of elements, and you want to drop all elements that don't match specific criteria from the Array, you're looking for Array's filter(isIncluded:) method. Let's say you have an array of words and you only want to keep the words that are longer than three characters: let words = ["hello", "world", "this", "is", "a", "list", "of", "strings"] let filtered = words.filter { word in return word.count >= 3 } // filtered is ["hello", "world", "this", "list", "strings"] The filter(isIncluded:) method takes a closure that is executed for every element in the source Array. If you return...

Read more...

Five tips to write better todos in Xcode

Published on: January 8, 2020

We all write the dreaded // TODO: and // FIXME: comments every once in a while. Sometimes we do it because we know our code can be better but we're not sure how, other times we don't have the time to write an optimal solution because of deadlines, and other times we just want to move on to more interesting problems to solve and we just slap a // TODO: on our code and call it a day. In today's post, I would like to give you five tips to make sure your todos eventually get fixed and don't end...

Read more...

Getting started with Combine

Published on: January 6, 2020

The Combine framework. Silently introduced, yet hugely important for iOS. It didn't get any attention during the big Keynote at WWDC 2019, but as soon as folks were in the sessions they knew that Combine was going to be huge. It implements a Functional Reactive Programming (FRP) paradigm that's similar to that of Rx which is implemented by RxSwift, except it's made by Apple and has native support on all Apple platforms as long as they are running iOS 13+, iPadOS 13+, macOS 10.15+, watchOS 6+ or tvOS 13+. The fact that Apple created their own FRP framework is a...

Read more...

Swift’s typealias explained with five examples

Published on: January 2, 2020

Swift grants developers the ability to shadow certain types with an alternative name using the typealias keyword. We can use this feature to create tuples and closures that look like types, or we can use them to provide alternative names for existing objects. In this post, we'll look at five ways in which typealiases can help you write cleaner and better code. 1. Improving readability with type aliases Perhaps this is the most obvious yet also somewhat underused way to use a typealias. When you have a type in your code that is very long or deeply nested, you could...

Read more...

Reversing an Array in Swift

Published on: January 1, 2020

You can reverse an Array, and any other Collection in Swift using the reverse method. For example var input = [1, 2, 3] print(input) // [1, 2, 3] input.reverse() print(input) // [3, 2, 1] The code above takes an array (input) and reverses it in-place using the reverse() method. This only works if your array is mutable. If you want to reverse an immutable array that's defined as let, or if you don't want to alter the original input you can use reversed() instead of reverse(): var input = [1, 2, 3] print(input) // [1, 2, 3] var reversed =...

Read more...

Year in review: 2019

Published on: December 30, 2019

It's the end of the year, and that means that we should all take a little bit of time to reflect on the past year. Some people like to reflect on their past year in a very statistical manner, they set measurable goals and they decide whether they met their goals at the end of the year. Personally, I don't really set goals per year. I simply strive to do the best I can every year. In this post, I would like to reflect on how 2019 has been for me, and I will share some of my plans for...

Read more...