Swiftly Coding
Quick Ways to Run Swift
Running Swift Code
For capturing your thoughts and quickly testing Swift code, there should be ways for us to jump into coding and see the results as soon as possible. The goal of this article is to list out the ways to run Swift code and compare their pros and cons.
There are several ways to run Swift code:
- Swift Playgrounds - quick and easy to start drafting Swift code with live preview.
- Xcode - more features for developing Swift applications.
- Swift REPL - interactive way to test Swift code.
- Online Swift Sandbox - run Swift code online without any setup.
Swift Playgrounds
Swift Playgrounds is a simple way to run Swift code.
It should be the easiest way to start drafting your Swift and especially SwiftUI code with live preview. In terms of functionality, it has fewer features compared to Xcode, but as the main purpose is for learning new things or trying out concepts, it is more than adequate.
Running Swift Playgrounds projects from Xcode
If you really want to have a full-fledged Swift development environment, you can open the project in Xcode.
You can find the Swift Playgrounds projects by right-clicking and selecting Show in Finder
to locate the project (.swiftpm
) file to open with Xcode.
Xcode
Xcode is the officially supported IDE for Swift, offering the most comprehensive features and developer experience. To quickly start writing Swift code, you can create either an Xcode Playground or a new project.
Xcode Playground
As discussed in my post about making the desktop your workspace, starting a new coding draft should feel as effortless as grabbing paper and pen to sketch ideas. Xcode Playground provides the most straightforward way to begin drafting Swift code.
You can start coding immediately by creating a new playground on your Desktop with the default name, or use Raycast to create a playground with a custom name like MyPlayground-11-10-2024
in one second. This approach eliminates the need to worry about setup or file organization.
Xcode Project
For more complex applications or when exploring architectural frameworks, creating a new project is the best option. This setup allows you to build and test more sophisticated code structures.
Swift REPL
Swift is a compiled language, but it also has a REPL (Read-Eval-Print Loop) feature. This means you can write code and see the result immediately.
Firstly, check if Swift is installed:
swift --version
Start REPL by typing swift
in the terminal:
swift
Even though Swift REPL is for interactive use, you can still create a .swift
file to run as a script:
echo 'print("Hello, World!")' > hello.swift
swift hello.swift
Or, if you want to compile and run a program, you can use swiftc
:
swiftc hello.swift -o hello
./hello
If you want to create a new project with a package to build more complex applications, you can use swift package init
:
mkdir MyProject
cd MyProject
swift package init --type executable
# Edit main.swift in Sources/MyProject/
swift run
With the package, you can manage dependencies and build the project more easily. You can also use Xcode to open the project and take advantage of Xcode’s features.
Online Swift Sandbox
Using Raycast
Using Raycast, you can quickly run Swift code with the following extensions:
- Swift Repl, start Swift REPL for you to quickly test Swift code.
- Xcode, which allows you to create a new playground file or open an existing one.