Creating a resizable and reusable view for a macOS app

Creating a macOS app allows us to use a whole different set of tools than creating an iOS app. In this article we’ll look at one example of a view we can only use at the mac: A Tag Cloud that reacts to resizing and places its tags according to the given space. This is a demo of TagCloudView:

Demo of TagCloudView with 4 tags.

First, we’re going to look at how to create the rows and how to place tags in them. But this initial setup will not allow resizing, we’ll handle this in the…


Using UndoManager to create an Undoable History

Undo and redo are two common functions found in many apps and your users may expect it in your app as well. Whenever it’s possible to manipulate data, your users will be grateful when they can undo their errors. Imagine using Xcode without the possibility to undo your latest change or redo it again — it would be a horror!

This gif shows an example of how undo and redo work:
The three buttons “Red”, “Blue” and “Yellow” change the color of the view above. …


Inspecting the content of a Swift source file in Swift

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

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

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

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

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

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…

David Piper

Working as an iOS and Android dev.

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