SW Swift

Swift: Running Code & Tools

Running Swift

swift main.swift              # compile and run a script
swiftc main.swift -o myapp    # compile to a binary
swift run                     # with Package.swift (SPM project)

swift runs .swift files directly (compiling on the fly). swiftc is the offline compiler that produces a standalone binary.

Xcode (the standard IDE)

Xcode is Apple's IDE for all Apple-platform development. It's available only on macOS and includes: - Source editor with Swift-specific refactoring - Interface Builder for drag-and-drop UI design - iOS/macOS/watchOS simulators - Instruments profiler - Swift Playgrounds for interactive experimentation

Other editors

  • VS Code — install the Swift extension by SSWG (Server Side Swift Working Group: sswg.swift-lang)
  • AppCode — JetBrains IDE (now discontinued — use Xcode or VS Code)
  • Neovim with sourcekit-lsp for language server support

Swift Package Manager (SPM)

SPM is the official Swift build system and package manager:

swift package init --type executable   # create a new project
swift package init --type library       # create a library
swift build                              # compile
swift test                               # run tests
swift package update                     # update deps

Package.swift — the manifest

// swift-tools-version:5.9
import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [
        .package(url: "https://github.com/vapor/vapor", from: "4.0.0")
    ],
    targets: [
        .executableTarget(name: "MyApp", dependencies: ["Vapor"])
    ]
)

Popular server-side frameworks

Framework Use Case
Vapor Full-featured server-side Swift framework
Kitura IBM's Swift web framework (archived but still used)
Hummingbird Lightweight HTTP server framework

Testing with XCTest

import XCTest

class MathTests: XCTestCase {
    func testAddition() {
        XCTAssertEqual(2 + 3, 5)
    }
}

Quick check below!