Run Swift



Developers are doing
great things with Swift.

Run Swift is a web tool where you can easily try Apple's Swift language online. Just paste any snippet and click Run. You may also Save your scripts and/or share with the world. If what you're looking for is an easy-to-use tool for your Mac, to quickly test and run Swift code, then you may want to have a look at our Run Swift app for MacOS. Swift command to get the Swift version, run Swift file with arguments, swiftc to make the executable file and the object file, link multiple object files to the module, import framework when compiling Swift file, create an executable package, Swift REPL to experiment with Swift, and use the LLDB debugger. The Swift Run Shoes from the adidas Originals Collection deliver light cushioning with an injection-molded EVA midsole. A sock-like construction delivers a close fit and supportive comfort for all-day wear.

Swift is a fast and efficient language that provides real-time feedback and can be seamlessly incorporated into existing Objective-C code. So developers are able to write safer, more reliable code, save time, and create even richer app experiences.

Great apps
built using Swift.

More and more developers are incorporating Swift code into their apps. And some are even building all-new apps entirely in Swift.

Better performance
equals better apps.

Swift apps more than live up to the name. For instance, a common search algorithm completes much faster using Swift.

10,000 integers found in a graph
using depth-first search algorithm*

Educators are adding Swift
to their curriculum.

Developers aren’t the only ones who’ve experienced the potential of Swift. Universities and academic institutions around the world teach with Swift and Xcode on Mac, empowering their students with the best tools to build amazing apps. And with Apple’s free Develop in Swift curriculum, the transition from introductory coding to app development with Swift has never been easier.

Colleges and universities that are incorporating Swift into their courses

Adidas swift run women
  • Aberystwyth University
  • Borough of Manhattan
    Community College
  • California Polytechnic State University
  • Central Piedmont Community College
  • Foothill College
  • Full Sail University
  • Houston Community College System
  • Ingésup
  • Lawson State Community College
  • Mesa Community College
  • Northwest Kansas Technical College
  • Plymouth University
  • RMIT University
  • Southern Methodist University
  • Stanford University
  • Technical University of Munich
  • Tecnológico de Monterrey
  • University of California, Santa Cruz

Swift is everywhere.
And now it’s open for everyone.

Swift is free and open source, and it’s available to a wide audience of developers, educators, and students under the Apache 2.0 open source license. We’re providing binaries for macOS and Linux that can compile code for iOS, macOS, watchOS, tvOS, and Linux. And to help Swift grow into an even more powerful language, we created a community where users can contribute directly to the Swift source code.

Get started with Swift.

The Definitive Book

Run Swift

Download The Swift Programming Language from the Apple Books Store free. Learn how Swift makes programming easier, more flexible, and more fun.

Download the free book

Xcode

Xcode is the Mac app used to build every other Mac app and every iOS app, too. It has all the tools you need to create an amazing app experience. And it’s available as a free download from the Mac App Store.

Download Xcode from the
Mac App Store

Developer Website

Get detailed technical information and an advanced overview of Swift. Check out the developer blog and stay up to date on the latest news. And enjoy free access to great resources like guides, videos, and sample code.

Run Swift Adidas

Learn more

Here, you’ll find information about the how to use the Swift programming language.

If you’re new to Swift, check out A Swift Tour inThe Swift Programming Language, for a quick introduction to themost important concepts and features of the language.

Installing Swift

The first step to using Swift is to download and installthe compiler and other required components.Go to the Download pageand follow the instructions for your target platform.

In order to follow along with the examples below,make sure to add Swift to your $PATH.

On macOS

The default location for the downloadable toolchain on macOS is/Library/Developer/Toolchains.You can make the latest installed toolchain available for use from the terminal with the following command:

To select any other installed toolchain, use its identifier in the TOOLCHAINSvariable. The identifier can be found in toolchain’s Info.plist file.

On Linux

  1. Install required dependencies:
Ubuntu 16.04Ubuntu 18.04Ubuntu 20.04CentOS 7CentOS 8Amazon Linux 2

If you installed the Swift toolchain on Linuxto a directory other than the system root,you will need to run the following command,using the actual path of your Swift installation:

On Windows

Visual Studio and Swift

You will need to install both the toolchain installer from theDownload page and Visual Studio 2019.

The following Visual Studio components are required:

ComponentVisual Studio ID
MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.25)1Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Windows Universal C RuntimeMicrosoft.VisualStudio.Component.Windows10SDK
Windows 10 SDK (10.0.17763.0)2Microsoft.VisualStudio.Component.Windows10SDK.17763

1 You may install a newer build toolset.
2 You may install a newer SDK instead.

The following additional Visual Studio components are recommended:

ComponentVisual Studio ID
C++ CMake tools for WindowsMicrosoft.VisualStudio.Component.VC.CMake.Project
Git for WindowsMicrosoft.VisualStudio.Component.Git
Python 3 64-bit (3.7.8)Component.CPython.x64

The default installation location for the toolchain on Windows is%SystemDrive%LibraryDeveloperToolchains.

Support Files

Note that you must use the x64 Native Tools for VS2019 Command Prompt to runthe toolchain. The x64 Native Tools for VS2019 Command Prompt runs theDevEnv script from Visual Studio that sets up the necessary environmentvariables to find the system headers.

In order to make the Windows SDK accessible to Swift, it is necessary to deploya few files into the Windows SDK. The following will modify your Visual StudioInstallation, and as such will require to be run from an (elevated)“Administrator” x86 Native Tools for VS2019 Command Prompt.

Because it is installing the files into the Visual Studio image, the files willneed to be copied each time Visual Studio is updated.

Swift Version

You can verify that you are running the expected version of Swiftby entering the swift command and passing the --version flag:

The -dev suffix on the version numberis used to indicate that it’s a development build,not a released version.

Using the REPL

If you run the swift command without any other arguments,you’ll launch the REPL, an interactive shellthat will read, evaluate, and print the resultsof any Swift code you enter.

Interacting with the REPL is a great way to experiment with Swift.For example, if you enter the expression 1 + 2,the result of the expression, 3, is printed on the next line:

You can assign values to constants and variables,and use them in subsequent lines.For instance, the String value Hello, world!can be assigned to the constant greeting,and then passed as an argument to the print(_:) function:

If you enter an invalid expression,the REPL will print an error showing where the problem occurred:

You can use the up-arrow and down-arrow keys ( and )to cycle through previous lines entered into the REPL.This allows you to make a slight change to a previous expressionwithout retyping the entire line,and is especially convenient for fixing errors like the one in the previous example:

Another useful feature of the REPLis that it can automatically suggest functions and methodsthat can be used in a particular context.For example, if you enter reafter a dot operator on a String valueand then hit the tab key (),the REPL will give a list of available completionslike remove(at:) and replaceSubrange(bounds:with:):

If you start a block of code,such as when iterating over an array with a for-in loop,the REPL will automatically indent the next line,and change the prompt character from > to .to indicate that code entered on that linewill only be evaluated when the entire code block is evaluated.

All of the functionality of Swift is available to you from the REPL,from writing control flow statementsto declaring and instantiating structures and classes.

You can also import any available system modules,such as Darwin on macOS and Glibc on Linux:

On macOS

On Linux

On Windows

The REPL depends on Python bindings. You must ensure that Python is availablein the path. The following command adds Python to the PATH so that it can beused:

Because the Windows installation separates out the SDK from the toolchain, a fewextra parameters must be passed to the REPL. This allows you to use multipledifferent SDKs with the same toolchain.

Using the Package Manager

Swift package manager provides a convention-based system forbuilding libraries and executables, and sharing code across different packages.

These examples assume you have made swift available in your path;see Installing for more information.Once available, you can invoke the package manager tools: swift package, swift run, swift build and swift test.

Creating a Package

To create a new Swift package, first create and enter a directory named Hello:

Every package must have a manifest file called Package.swift in its root directory.You can create a minimal package named Hello using:

Run Swift Nike

By default the init command will create a library package directory structure:

You can use swift build to build a package. This will download, resolve and compile dependencies mentionedin the manifest file Package.swift.

To run the tests for a package, use: swift test

Building an Executable

A target is considered as an executable if it contains a file named main.swift.The package manager will compile that file into a binary executable.

In this example,the package will produce an executable named Hellothat outputs “Hello, world!”.

First create and enter a directory called Hello:

Now run the swift package’s init command with executable type:

Use the swift run command to build and run the executable:

Note: Since there is only one executable in this package, we can omit theexecutable name from the swift run command.

Nike Run Swift Se

You can also compile the package by running the swift build command and then runthe binary from .build directory:

As a next step, let’s define a new sayHello(name:) functionin a new source file, and have the executable call thatinstead of calling print(_:) directly.

Working with Multiple Source Files

Create a new file in the Sources/Hello directory called Greeter.swift,and enter the following code:

The sayHello(name:) function takes a single String argumentand prints our “Hello” greeting before, substituting the word “World”with the function argument.

Now, open main.swift again, and replace the existing contents with the following code:

Rather than using a hardcoded name as before,main.swift now reads from the command line arguments.And instead of invoking print(_:) directly,main.swift now calls the sayHello(name:) method.Because the method is part of the Hello module,no import statement is necessary.

Run swift run and try out the new version of Hello:

To learn about the Swift Package Manager,including how to build modules, import dependencies, and map system libraries,see the Swift Package Manager section of the website.

Using the LLDB Debugger

You can use the LLDB debugger torun Swift programs step-by-step,set breakpoints,and inspect and modify program state.

As an example,consider the following Swift code,which defines a factorial(n:) function,and prints the result of calling that function:

Create a file named Factorial.swift with the code above,and run the swiftc command,passing the filename as a command line argument,along with the -g option to generate debug information.This will create an executable named Factorialin the current directory.

Instead of running the Factorial program directly,run it through the LLDB debuggerby passing it as a command line argument to the lldb command.

This will start an interactive consolethat allows you to run LLDB commands.

For more information about LLDB commands,see the LLDB Tutorial.

Set a breakpoint on line 2 of the factorial(n:) functionwith the breakpoint set (b) command,to have the process break each time the function is executed.

Run the process with the run (r) command.The process will stop at the call site of the factorial(n:) function.

Use the print (p) commandto inspect the value of the n parameter.

The print command can evaluate Swift expressions as well.

Nike Run Swift

Use the backtrace (bt) commandto show the frames leading to factorial(n:) being called.

Use the continue (c) commandto resume the process until the breakpoint is hit again.

Use the print (p) command againto inspect the value of the n parameterfor the second call to factorial(n:).

Use the breakpoint disable (br di) commandto disable all breakpointsand the continue (c) commandto have the process run until it exits.

Run Swift Shoes

Now that you’ve been introduced to the Swift REPL, build system, and debugger,here are a few suggestions for what to do next:

Nike Run Swift Shoes

  • Check out the Package Manager project pagefor a deep dive into the Swift build system and package manager.
  • Read Contributing to Swiftto learn about the different ways you can participate in the Swift community.
  • Go to developer.apple.com/swiftfor additional Swift resources, including videos, sample code, and playgrounds.