Recent articles

Jump to a random post

How to sort an Array based on a property of an element in Swift?

Published on: January 20, 2020

This post is a short demo of using Swift's sort() and sort(by:) methods. For a more comprehensive overview of sorting, and some background information on how Swift's sorting works I recommend you take a look at this updated post The easiest way to sort an Array in Swift is to use the sort method. This method is available for all Arrays that have Comparable elements and it sorts your array in place: var words = ["hello", "world", "this", "is", "a", "list", "of", "strings"] words.sort() // words is now ["a", "hello", "is", "list", "of", "strings", "this", "world"] This modifies the input...

Read more...

Refactoring a networking layer to use Combine

Published on: January 20, 2020

In the past two weeks I have introduced you to Combine and I've shown you in detail how Publishers and Subscribers work in Combine. This week I want to take a more practical route and explore Combine in a real-world setting. A while ago, I published a post that explained how you can architect and build a networking layer in Swift without any third-party dependencies. If you haven't seen that post before, and want to be able to properly follow along with this post, I recommend that you skim over it and examine the end result of that post. This...

Read more...

How to add an element to the start of an Array in Swift?

Published on: January 19, 2020

You can use Array's insert(_:at:) method to insert a new element at the start, or any other arbitrary position of an Array: var array = ["world"] array.insert("hello", at: 0) // array is now ["hello", "world"] Make sure that the position that you pass to the at: argument isn't larger than the array's current last index plus one. For example: // this is fine var array = ["hello"] array.insert("world", at: 1) // this will crash var array = ["hello"] array.insert("world", at: 2) If you have any questions about this tip, or if you have feedback for me, don't hesitate to reach...

Read more...

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