Swift Environment Setup
Swift is an open-source programming language used to develop OS X and iOS applications.
Before officially developing applications, we need to set up a Swift development environment so that we can use various development tools and languages in a friendlier way for rapid application development. Since the Swift development environment must run on OS X, its setup differs from the Windows environment; let's learn together how to set up the Swift development environment.
Prerequisites for successfully setting up the Swift development environment:
- You must have a Mac, because the integrated development environment Xcode only runs on OS X.
- The system must be OS 10.9.3 or later.
- The computer must have the Xcode IDE installed.
Installing Xcode 16
For developing Apple platform apps on macOS, Xcode is the only official complete IDE.
Open the App Store, search for Xcode to install it, or go to developer.apple.com/xcode to download the installer.
After downloading, double-click the downloaded dmg file to install; once installed, move the Xcode icon to the Applications folder.
You can also search for xcode in the App Store to install it, as shown below:
The Swift compiler bundled with Xcode 16 is 6.x, supporting Swift 6 language mode and backward-compatible with Swift 5 mode.
The first launch after installation requires agreeing to the license agreement and installing additional components, which takes a few minutes.
To verify the installation succeeded, run in the terminal:
$ swift --version
swift-driver version: 1.148.6 Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101)
Target: arm64-apple-macosx26.0
As long as you see Apple Swift version and the corresponding target platform, the toolchain is ready.
Note:Xcode's command-line tools need to be activated separately once. If it reports that it can't find swift command, run xcode-select --install to install the command-line tools, then run sudo xcode-select -s /Applications/Xcode.app to specify the path.
Your First Swift Program
Once Xcode is installed, we can start writing Swift code.
Next we open Xcode from the Applications folder, then select File => New => Playground at the top of the screen.
Then set a name for the playground and choose the iOS platform.

Swift's playground is like an interactive document; it's for practicing Swift—you write a line of code and get a line of result (on the right), letting you view results in real time. It's a great tool for learning Swift! 
The following is the default code in a Swift Playground window:
import UIKit
var str = "Hello, playground"
If you want to create an OS X program, you need to import the Cocoa package **import Cocoa **, as shown below:
import Cocoa
var str = "Hello, playground"
After the above program is loaded, the execution result is shown on the right side of the Playground window:
Hello, playground
At this point you've completed your first Swift program; congratulations on getting started.
Create your first project
1. Open Xcode and select File => New => Project
2. We choose a "Single View Application" and click "next" to create a simple sample app.
3. Next we enter the product name (ProductName), company name (Organization Name), organization identifier prefix (Organization identifier), and also choose the development language (Language) and device (Devices).
Language has two options: Objective-C and Swift; since we're learning Swift, of course we choose Swift. Click "Next" to continue.
4. Choose the directory to store it; if you want to use Git source control, check Source Control's "create git repository on My Mac". Click create to create the project.
5. After the project is created, a sample file is generated by default; you can see Swift merged OC's h and m files into one file (i.e., the swift-suffixed file). Main.storyboard is equivalent to an xib file but has more features than xib.
6. Open main.storyboard and by default you'll see a simple blank app interface, sized as a tablet interface. If developers only need to develop apps compatible with iPhone, they can uncheck Use Auto Layout (checked by default).
7. A dialog pops up asking us to choose the interface size, iPhone or iPad. We choose the iPhone size.
8. You can see the interface size has changed to the width and height of an iPhone.
You can remember the interface dimensions to make it easier to calculate positions in layout later:
![]()
9. Let's add some content to the interface: find the Text control at the bottom right, drag it onto the storyboard, and double-click to enter the text "Hello World!".
Run the simulator (shortcut command+R or choose Product => Run from the menu bar).
At this point our first Swift project is complete.
Creating a New SwiftUI Project
Xcode 16 recommends SwiftUI by default when creating a new project; it's Apple's currently promoted declarative UI framework.
Launch Xcode, choose File → New → Project, select App under the iOS tab, then fill in as below.
| Option | Suggested value | Description |
|---|---|---|
| Product Name | RunoobDemo | Project name, also used as the module name |
| Team | Personal account or None | Only required for on-device debugging |
| Organization Identifier | com.runoob | Reverse domain name, used to generate the Bundle ID |
| Interface | SwiftUI | The other option is Storyboard |
| Language | Swift | The other option is Objective-C |
The Interface option decides which UI framework the project uses; choosing wrong won't cause an error, but the subsequent code will be completely different.
SwiftUI describes the interface with declarative code, and the interface updates automatically with data; Storyboard uses visual drag-and-drop plus XML to describe the interface, then connects to code via IBOutlets.
For new projects, SwiftUI is recommended because it's Apple's current focus, and new system features are provided on SwiftUI first.
After creation, Xcode automatically generates two files: RunoobDemoApp.swift and ContentView.swift.
Click the run button in the top left, or press Command + R, and the simulator will launch showing the default interface.
Using Playground
Playground is the best tool for practicing syntax: after entering code, results appear in real time on the right, with no manual compile or run needed.
In Xcode choose File → New → Playground, select the Blank template, and save it locally.
Enter a line print("RUNOOB") and the result panel on the right will immediately show RUNOOB.
Playground also has two special directories on the left:
| directory | Purpose | Notes |
|---|---|---|
| Sources | Stores helper code and custom types | Code here must be public to be called from the main page |
| Resources | Stores resources such as images, JSON, and audio | Read with Bundle.main |
Playground also supports importing SwiftUI to preview interfaces directly, suitable for quickly trying out UI effects.
Note:In Playground, top-level code can have expressions whose results display automatically, but when using the swift command to run .swift a file, ordinary expressions aren't printed automatically; you must explicitly call print。
Running from the command line
You can run Swift without opening Xcode, which is fastest for verifying a small piece of code.
First create a file hello.swift with any editor:
Example
import Foundation
// The entry point of a command-line program is the code at the file's top level
let site
=
"RUNOOB"
(
"Hello, (site)!"
)
Run the source file directly with the swift command:
$ swift hello.swift
Hello, RUNOOB!
The swift command executes the file as a script, good for quick verification, but it recompiles every time, so it's not suitable for large projects.
Compile into an executable with swiftc
If you want to compile the code into a standalone binary, use swiftc.
$ swiftc hello.swift -o hello
$ ./hello
Hello, RUNOOB!
-o specifies the output filename; the generated executable can be distributed directly to other machines on the same platform.
When compilation fails, swiftc gives error messages precise down to the line and column, for example when assigning a string to an Int:
$ swiftc bad.swift
bad.swift:1:14: error: cannot convert value of type 'String' to specified type 'Int'
Interactive input with swift repl
swift repl without any arguments starts an interactive environment where you enter a line and it executes a line.
$ swift repl
Welcome to Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101).
Type :help for assistance.
1> let site = "RUNOOB"
site: String = "RUNOOB"
2> print(site)
RUNOOB
3> :quit
REPL prints the value of an expression together with its inferred type, suitable for observing type inference results.
Enter :quit or press Control + D to exit.
First Taste of Swift Package Manager
Swift Package Manager (SwiftPM for short) is Swift's official package management and build tool, and Xcode has it built in.
To create a new executable package, run in an empty directory:
$ swift package init --type executable
Creating executable package: demo
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/demo/demo.swift
Creating Tests/
Creating Tests/demoTests/
Creating Tests/demoTests/demoTests.swift
The generated directory structure is fixed: source files must go under Sources and tests under Tests.
demo/
├── Package.swift
├── Sources/
│ └── demo/
│ └── demo.swift
└── Tests/
└── demoTests/
└── demoTests.swift
The generated Package.swift describes the package's name, targets, and language mode:
Example
// swift-tools-version: 6.3
import PackageDescription
let package
=
Package
(
name
:
"demo"
,
targets
:
[
// Executable target: produces a command-line program when compiled
.
executableTarget
(
name
:
"demo"
)
,
// Test target: depends on the main target
.
testTarget
(
name
:
"demoTests"
,
dependencies
:
[
"demo"
]
)
,
]
,
// Use Swift 6 language mode, enabling strict concurrency checking
swiftLanguageModes
:
[
.
v6
]
)
The default generated entry file marks a struct with @main, which is the recommended practice for SwiftPM executable targets:
Example
// File path: Sources/demo/demo.swift
@main
struct
demo
{
static
func main
(
)
{
(
"Hello, RUNOOB!"
)
}
}
Compile and run with swift run:
$ swift run
Build of product 'demo' complete! (13.94s)
Hello, RUNOOB!
The three commonly used commands are swift build to compile only, swift run to compile and run, and swift test to run tests.
Swift on Linux and Windows
Since going open source in 2015, Swift has gradually added support for Linux and Windows, with officially provided prebuilt toolchains.
to swift.org/install You can download installers for each platform.
| Platform | Installation method | Description |
|---|---|---|
| Ubuntu | Official tarball or apt repository | The Linux distribution with the most complete support |
| Other Linux | Official tarball | Need to install dependency libraries yourself |
| Windows | Official installer | Requires Visual Studio components |
Linux and Windows don't have Xcode, nor Apple platform frameworks like UIKit and SwiftUI.
But core capabilities such as Foundation, SwiftPM, and async/await all work normally, so writing server programs and command-line tools is completely fine.
Note:On Linux Foundation It's implemented by swift-corelibs-foundation, with some APIs differing from Apple platforms; cross-platform projects should avoid relying on Apple-specific types.
Common questions
Below are some of the most common problems encountered during environment setup.
It reports command not found: swift
This means the command-line tools aren't installed properly or the path isn't set; run xcode-select --install to install, then sudo xcode-select -s /Applications/Xcode.app to point to the correct Xcode.
The swift command recompiles every time; can it be executed directly?
After compiling an executable with swiftc, add execute permission to run it directly; you can also write #!/usr/bin/swift as the first line of a script to execute it as a script.
What to do if the Xcode download is too slow
can be done during developer.apple.com/download/all Download the xip installer, speed it up with a download tool, then install manually.
Playground keeps spinning without showing results
Usually Playground is stuck compiling; press Command + B to trigger a build manually, or close and reopen it.
Can you install only the command-line tools without the full Xcode?
Yes; xcode-select --install installs Command Line Tools, which include swift and swiftc—enough to learn syntax and write command-line programs, but without an IDE or simulator.