Skip to content

100 Days of Swift

07/17/24

This is the story how I got, quite unexpectedly, into to the Swift programming language. It all started on a sunny summer day with a visit to the river.

I was doing some experiments with long-term exposure with my DSLR camera. If you take a picture of water with a fast shutter speed, you freeze the motion of the water in the image. On the surface of the water you can see every ripple and every reflection. The longer you make the exposure, the more the ripples and water movements blend together until you get a silky smooth and perfectly flat surface, so called motion-blur. If all you do is increasing the exposure, there is one problem. Your picture will be plain white. To solve that, you can use a special kind of filter you put on the lens. It’s called a neutral density filter, or ND-filter for short. It reduces the intensity of the light without changing the colour. Think of sunglasses, but without the tint, just perfectly grey, in most cases.

ND-filters come in different levels of density. They are denoted using numbers, sometimes with the f-stop number, more on that later, sometimes with the ND-number as a power of two. Mine use the ND-number that denotes the fraction of the light transmitted as a power of two. For example, there is the ND2 filter. ND2 means that it blocks exactly half of the light going through the filter, the same as reducing the size of the lens opening to one half of its original size. This corresponds to the f-stop number 1, we say that the filter reduces the light by one “stop”. If I put that filter on the camera that produced a perfectly exposed image before, and for simplicity I keep the aperture level fixed, I need to double the exposure time to get an image with the same light exposure. This sounds a bit complicated, but it’s actually a very simple mechanism. There is half the amount of light, so we need to double the time to get the same image from or sensor.

Another example would be the ND1024 filter, which is sometimes called ND1000. It reduces the amount of light to 1/1024, or by 10 f-stops. This means I have to double the exposure time ten times to get a similarly exposed image. Back to the river I was photographing. I set up my aperture and focal length and used the light meter of the camera to set my shutter speed so that I get a perfectly exposed image. Let’s assume it was 1/50 seconds. I put on the ND1000 filter, knowing that it will increase the shutter speed by enough to get as much motion-blur on the water as possible. Taking a picture right now would result in a black image. I have to increase the shutter speed by factor 1024, which is exactly 20.480 seconds. Fortunately my camera has a “bulb” mode where I can set really long exposure times. I took the picture, and it was perfectly exposed with a silky smooth water surface.

This example may sound like calculating the exposure with ND-filters is an easy thing to do, but it can quickly get really complicated. You can change the aperture too, which affects the exposure just the same as the shutter speed does. You can also combine multiple ND-filters. When standing next to the river ready for shooting a picture, I don’t want to take out a paper, a pen and the calculator to sit down and calculate the exposure time and aperture to use. So I do what every sane person would do - I take out my phone, open the App Store and look for an ND-calculator app.

Back when I was using Android there was a nice, free app that did everything I wanted, without throwing ads in my face. On the iPhone it’s a different story. I couldn’t find a good app that was not logging all my data, asking for money or was filled with ads. That got me thinking. Since the UI is simple and the app is not more than a special kind of calculator, being a developer by trade, shouldn’t I just build the app myself? Sure, why not?

The question that followed immediately was the one of progressive web app vs. native app. A PWA would be a good choice. It would be available over the internet, work on desktops, on Android, and as a senior web developer it would be easy to do for me. But there are also downsides to PWAs. They are not as widely supported as they should be and installing them is a process people are rarely used to. Where is the fun in doing something you already know? Native app it is!

I never cared much for native iOS app development before, so everything about it was new to me. Apparently you use the Swift language to develop Apple apps. Swift is open-source, and the compiler is based on LLVM. Although it is mainly used for apps running on Apple devices, it can target all major platforms, like Windows and Linux. Modern Swift can even run on embedded systems.

Swift is a surprisingly nice language with many modern features. I think it could be one of the two contenders for a modern, safe C++ alternative, next to Rust. One of the benefits it has over Rust is that it is backed by Apple, in addition to the open-source community. Another one is the major UI framework you get out-of-the-box, SwiftUI. It’s the framework you should probably use to write a iOS app, I suppose.

SwiftUI uses many of the well-known patterns familiar to anyone working in modern web-development. It comes with a large set of tools to do virtually anything a UI needs. It provides you with overlays, navigations, typography helpers, layout builders, and much more.

I’m looking forward using all of that soon and a iOS app idea is a good way to improve knowledge with languages and frameworks. But where to start? Diving into app development without any deeper knowledge of a language or the UI toolkit can be daunting. You can lose traction very fast. Browsing the web for resources on learning Swift I came across a website about 100 days of SwiftUI and I want to use that idea for my own purpose. I might even follow that SwiftUI course, but for now I have other plans. I put together a list of rules to follow in the next couple of months.

  1. Try to spend exactly 1 hour of programming Swift each day, if possible.
  2. Use any study source you want, be it coding exercises, reading books, or creating custom apps.
  3. If you frequently get bored, switch your study source to something more complicated.
  4. If you frequently get stuck, switch your study source to something simpler.
  5. Post your progress on Mastodon daily, adding a short reflection on the current day of study.

I’m currently on day 29 of 100. I am following the Swift track on Exercism and I have read parts of the official Swift book. Once I get bored with that, I will probably move on to build some CLI ideas on this “projectbook” website I recently discovered.

Feel free to join me.