Inspecting the content of a Swift source file in Swift

Image for post
Image for post
Photo by Sereja Ris on Unsplash

SourceKitten is an open-source project to parse the content of and get detailed information about a Swift file. It’s used in many tools you may use regularly, like SwiftLint and Jazzy.

When looking at its readme file, it seems like it’s only a command-line tool. However, SourceKitten also includes a framework called SourceKittenFramework that we can use to build programs to analyse and inspect Swift code ourselves. It’s only usable in a Mac app and does not support iOS, tvOS, or watchOS.

In this article, we will first start by integrating the framework into our project. Next, we will see…


How to draw with your finger and export an image

Image for post
Image for post
Image by Aline Ponce from Pixabay

In this short article, we will explore how to add a view to our apps, where users can draw with their finger.

Creating a drawing is quite easy. First, we will see how to get the positions of touches and track movements. The next step is to connect these points in a CGContext to get our drawing. Finally, we will see how to reset and export the created drawing as a UIImage.

Getting the Location of Touches

We start by tracking the touches inside of the view.

// 1 — First, we create a reusable UIView subclass to wrap the drawing function. In this…


How to use and convert units in a type-safe way

Image for post
Image for post
Image by William Warby from unsplash.

Whenever we are dealing with measurements in our apps and need to convert one unit to another (e.g. an imperial unit like inches to a metric unit like meters), we can write the conversion ourselves. However, this can get quite complicated and error-prone.

Instead, Apple provides a convenient way of dealing with units, measurements, and their conversion by providing a struct calledMeasurement. A measurement holds a value and a unit, and it can be converted to other units of the same dimension.

In this article, we will explore Swift’s Dimension, Unit, and Measurement. To do this, we will first see…


Add a show/hide password button to your password text field

Image for post
Image for post
Photo by Shane Avery on Unsplash

Many big apps — like YouTube, Twitter, and Amazon — allow their users to switch between a secured text field and a normal text field whenever a password needs to be entered. This is a good user experience because it’ll be very frustrating for your users to need multiple tries to log in just because they have a complex password and made a spelling mistake.

In this piece, we’ll explore how to recreate this function in SwiftUI. First, we’ll see how to use a SecureField to obscure the entered text. …


Splitting your views for more functionality

Image for post
Image for post
Photo by Pietro De Grandi on Unsplash

Many macOS apps — like Safari, Finder, Mail, and Xcode — use split views to divide their content into separate areas. This allows a user to decide how to size different parts of the app themselves.

Coming from an iOS background, I was surprised how many fewer references, articles, and documentation exist for macOS development; thus, I decided to write down my experiments on how to use an NSSplitView.

In this tutorial, we’ll first add a split view via a storyboard and see how to add more panels. Next, we’ll explore how to recreate Xcode’s segmented control to show and…


How KeyPaths improve map, compactMap, flatMap, and filter

Image for post
Image for post
Photo by Aleksandra Boguslawska on Unsplash

When working with collections like arrays or sets, we can use so-called higher-order functions like map to transform each element of the collection or filter to get a subset of the collection’s elements.

A higher-order function is a function that takes or returns other functions. If you are new to them, the site Use Your Loaf gives a great overview.

In this article, we will look at a new way of using them with Swift’s KeyPaths.

First, we will explore KeyPaths to see what they are and how they can be used. Later, we will look at new implementations of…


Using k-d Trees in Swift

Image for post
Image for post
Real trees are nice to look at, but not related to k-d trees. Image by Valiphotos from Pixabay

Even in simple apps we are often dealing with multidimensional data. These are elements which contain more than one value, e.g. a struct User with the properties name, age, income and address. Performing queries on these elements based on multiple parameter can be quite time consuming, especially when searching through 100 000 or even 1 000 000 elements.

In this article we will explore socalled k-d trees to increase the performance of our apps. First, we will see what a k-d tree is and how it structures and stores data. Next, we will look at the framework KDTree, which allows…


Plotting data with the framework Charts

Image for post
Image for post
Example of a Radar Chart made with Charts.

In this article, we will explore how to create radar charts in Swift to plot our data.

A radar chart is a special kind of chart with a web-like structure and star-shaped forms, thus, sometimes it’s also called spiderweb chart or star plot. A good example can be seen in the image above.

We won’t create such charts from scratch, instead we will use the framework Charts, which contains eight different types of charts, including these radar charts.

To see how to use this framework, we will try to recreate the chart from above, which is included in the project’s…


Improving the User Experience by Adding a Section Index

Once the amount of sections and rows in an UITableView becomes too large, your users will have a hard time finding and scrolling to a particular entry. This problem can be solved by adding a section index to your tableview, so that the user can jump straight to a section with only one single tap. Apple uses such indices whenever they need to show long lists, e.g. in Music, Contacts or iTunes U.

In this article we will see how easy it is to add such an index.

Creating an UITableView

Before we add a section index, we first need to have an…


Defining how to manipulate your data with property wrappers

Image for post
Image for post
Wrapping properties like they were candies— Image by StockSnap from pixabay

In this article, we will explore three property wrappers to enforce policies for our properties. By using them, we can make sure these properties are only used as they should be.

The first property wrapper will assure that encapsulated values can expire and are inaccessible after a given time has elapsed. In a second example, we will create one that ensures a value can only be updated once.

Each following update of this value will fail. The final property wrapper makes sure that the wrapped value is only changed within predefined steps.

For each of these wrappers, we will look…

David Piper

Working as an iOS and Android dev. Also going to finish my master degree in computer science anytime soon now!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store