VS Code Setup for Swift Project

VS Code is not an IDE (Integrated Development Environment). It is code editor. So If you want to run and debug code, have to do some extra steps.

I highly recommend reading this article. You may think I know what VSCode is. But some parts you may not know.

Step 1. Download and Install VS Code

Step 2. Install command line tool

Step 3. Open Terminal and Create a folder

cd ~
mkdir vscode
cd vscode
code .

Step 4. Install Official VSCode Swift Extension

Open Extensions Marketplace

  • command + shift + x
  • Install Swift Extension

Step 5. Create New Swift Project

command + shift + p

  • select Swift: Create New Project…
  • Select Executable (Yellow box)
  • Create a New Folder and Enter Folder name
  • Click Open

Step 6. Let’s run a swift program

I set executable project name as swift-example.

It consists of

  • .build
  • .vscode
  • Sources
  • Tests

I’ll explain details later in this post. Let’s run a swift program first.

When you click Sources/{project name} you will see this main function.

Following the arrows. You can see the Debug Console output.

Step 7. Set a breakpoint and debug swift program

Set a breakpoint. A breakpoint pauses the program before a specific line is executed. You can also use Command + F9 to add or remove it.

Breakpoints Panel

You can see Breakpoints options, Call Stack and Variables. The F5 shortcut starts debugging, while Shift + F5 stops the current debugging session.

The Breakpoints panel lists all breakpoints configured for the current debugging session.

The checkbox next to a breakpoint enables or disables it without deleting it.

The panel may also contain exception breakpoints such as:

  • Swift Catch: Pause when a Swift error is caught.
  • Swift Throw: Pause when a Swift error is thrown.
  • C++ Catch and C++ Throw: Pause on C++ exceptions.
  • Objective-C Catch and Objective-C Throw: Pause on Objective-C exceptions.

For a Swift executable project, the most important breakpoint is usually the normal source breakpoint placed next to a line of Swift code. The C++ and Objective-C breakpoint options are generally not needed for a beginner Swift project.

Debug Toolbar

  • Continue: Continue running the program until the next breakpoint.
  • Step Over: Execute the current line 7 and move to the next line. If the current line calls a function, the debugger does not enter that function.
  • Step Into: Enter the function called on the current line.
  • Step Out: Finish the current function and return to the line that called it.
  • Restart: Stop and start the debugging session again.
  • Stop: End the debugging session.

Variables

The Variables panel displays values that are currently available during the debugging session.

Locals

Locals shows variables that belong to the current function or scope.

For example:

func greet(name: String) {
let message = "Hello, \(name)"
print(message)
}

When the debugger pauses inside greet, name and message can appear under Locals.

Globals

Globals contains global variables and values that are accessible from a broader scope.

Registers

Registers displays low-level CPU register values. This information is mainly useful for advanced debugging and is usually not needed when learning Swift.

Watch

The Watch panel lets you monitor a specific expression while the program is paused.

For example, you can add:

name

or:

total + tax

The debugger evaluates the expression and displays its current value.

Watch expressions are useful when you want to repeatedly monitor a value while stepping through multiple lines of code.

Call Stack

The Call Stack shows the chain of function calls that led to the current execution point.

For example:

main()
└── calculateTotal()
└── applyDiscount()

This means that:

  1. main() called calculateTotal();
  2. calculateTotal() called applyDiscount();
  3. the program is currently paused inside applyDiscount().

Each item in the Call Stack is called a stack frame. Selecting a different stack frame allows you to inspect the variables and source code belonging to that function.

In the screenshot, the current stack frame is:

static swift_example.main()

This means that the program is currently executing the main() entry point of the executable.

The debugger may initially show only a few stack frames. Selecting Show More Stack Frames loads additional frames, including internal runtime or library calls.

Step 8. Folder Structure

.build

The .build directory is automatically generated by Swift Package Manager. It is used during compilation and debugging.

You normally do not need to edit the files inside this directory manually.

artifacts

Stores downloaded or built binary artifacts used by package dependencies.

For example, a package may provide a prebuilt binary library instead of Swift source code.

checkouts

Contains the source code of package dependencies checked out from Git repositories.

If the project does not use external packages, this folder may be empty or absent.

debug

Contains debug-build products.

When you run:

swift build

the compiled executable and related files are usually placed somewhere under this directory.

index-build

Contains index information used by SourceKit-LSP.

VS Code uses this information to provide features such as code completion, jump to definition, and symbol navigation.

out

Contains intermediate build output generated while SwiftPM is compiling the project.

These are temporary build files and are not normally edited manually.

repositories

Stores local repository information and cached data for Swift package dependencies.

It helps Swift Package Manager manage dependencies efficiently.

.buildSystem_debug

An internal file used by the Swift build system for debugging or build-system state information.

It is generated automatically and should not be edited manually.

.lock

A lock file used to prevent multiple SwiftPM processes from modifying the build directory at the same time.

CACHEDIR.TAG

A standard marker file indicating that .build is a cache directory.

It helps backup tools and other utilities recognize that the directory contains generated cache data.

manifest.pif

A generated project description file used by SwiftPM and related build tools.

It contains information about the package manifest and build configuration.

workspace-state.json

Stores SwiftPM workspace state, including information about package dependencies and resolved workspace data.

.vscode/launch.json

{
"configurations": [
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:swift-example}",
"name": "Debug swift_example",
"target": "swift_example",
"configuration": "debug",
"preLaunchTask": "swift: Build Debug swift_example"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:swift-example}",
"name": "Release swift_example",
"target": "swift_example",
"configuration": "release",
"preLaunchTask": "swift: Build Release swift_example"
}
]
}

The .vscode directory contains VS Code workspace-specific configuration files.

launch.json stores debugging configurations. It tells VS Code which Swift executable to launch when you start a debugging session.

For example, when you press F5, VS Code can use this file to:

  • build the Swift executable;
  • launch the program;
  • attach the debugger;
  • stop execution at breakpoints.

This file is useful for configuring program arguments, the working directory, and the executable path.

Conclusion

Next topic is how to setup iOS Project on VS Code

Comments

One response to “VS Code Setup for Swift Project”

  1. […] VS Code Setup for Swift Project […]

Leave a Reply

Discover more from Shawn

Subscribe now to keep reading and get access to the full archive.

Continue reading