Swift fundamentals

Removing duplicate values from an array in Swift

Published on: March 18, 2020

Arrays in Swift can hold on to all kinds of data. A common desire developers have when they use arrays, is to remove duplicate values from their arrays. Doing this is, unfortunately, not trivial. Objects that you store in an array are not guaranteed to be comparable. This means that it's not always possible to determine whether two objects are the same. For example, the following model is not comparable: struct Point { let x: Int let y: Int } However, a keen eye might notice that two instances of Point could easily be compared and two points that are...

Read more...

What are computed properties in Swift and when should you use them?

Published on: March 9, 2020

One of Swift's incredibly useful features is its ability to dynamically compute the value of a property through a computed property. While this is a super handy feature, it can also be a source of confusion for newcomers to the language. A computed property can look a bit strange if you haven't seen one before; especially when you are learning about custom get and set closures for properties at the same time. In this week's post, I would like to take some time to explain computed properties in-depth so you can begin using them in your codebase with confidence. By...

Read more...

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

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

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

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

When to use weak self and why

Published on: November 6, 2019

We all want to write good, beautiful and stable code. This includes preventing memory leaks, which we can, using [weak self] when writing a closure that needs access to self. But what's the real reason for needing this weak capture? And do we need it all the time? In this week's Quick Tip, I want to help you find an answer to this question so you can make more informed decisions about your capture lists in the future. This post contains the following topics: Understanding what a capture list is Understanding different kinds of captures Knowing when to rely on...

Read more...

When to use map, flatMap and compactMap in Swift

Published on: October 23, 2019

Any time you deal with a list of data, and you want to transform the elements in this list to a different type of element, there are several ways for you to achieve your goal. In this week’s Quick Tip, I will show you three popular transformation methods with similar names but vastly different applications and results. By the end of this post, you will understand exactly what map, flatMap and compactMap are and what they do. You will also be able to decide which flavor of map to use depending on your goals. Let’s dive right in by exploring...

Read more...