Getting Started with Swift

I decided to use some holidays I had to learn the Swift programming language. Below are the steps I followed to get started (and some gotchas I ran into) which is often the hardest part. I did not want to install Xcode so I only downloaded the Universal .pkg file from this link. I installed it without any issues but when I tried to run a sample program, I immediately got this error:

`PackageDescription` could not be found

After lot of debugging and searching online (answers to Swift problems are very hard to find because compared to Java, Python etc. it has much smaller user base), it seems this problem happens when you had a version of Swift that you unwittingly installed previously when you installed Xcode command line tools. E.g., in my case before installing Swift I had installed git which required me to install the Xcode c.l.t. Now I had two versions of Swift on my system.

Binary downloaded from Swift website:

% /Library/Developer/Toolchains/swift-5.7.2-RELEASE.xctoolchain/usr/bin/swift --version
Apple Swift version 5.7.2 (swift-5.7.2-RELEASE)
Target: arm64-apple-macosx13.0

Binary that got installed with Xcode Command Line Tools:

% /usr/bin/swift --version
Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: arm64-apple-darwin22.2.0

By default I was running /usr/bin/swift which I think came with Xcode c.l.t. When I changed to /Library/Developer/Toolchains/swift-5.7.2-RELEASE.xctoolchain/usr/bin/swift the problem went away. Its going to be tedious to have to type that long string every time you want to run Swift so you can add following line to your ~/.zshrc to use a shortcut:

alias swift=/Library/Developer/Toolchains/swift-5.7.2-RELEASE.xctoolchain/usr/bin/swift

The nasty problem I found out later is that if you do NOT download Xcode, you won’t be able to run any tests using the xctest command. See this. I was actually trying to download Xcode initially but before I could do so Apple wanted to know my address and other information. In all their setup screens they say they take your privacy very seriously and then force you to disclose your private information to them for no reason. Why should I have to tell Apple my address when all I wanted to do was download Xcode?

It doesn’t end here. To run the REPL swift repl, I had to use the Swift that comes with Xcode c.l.t. Using the other Swift gave me error. Hope it helps someone else who ends up with a setup like mine.

This entry was posted in Computers, programming, Software and tagged . Bookmark the permalink.

Leave a comment