print("Welcome to Online IDE!")
print("Start coding in Swift...")

Press Run or Ctrl ↵ to execute main.swift.
Type program input here while it runs.

Swift

Online Swift Compiler

Your code is compiled with swiftc from Swift 6.4 on Linux and runs as a native binary, with the standard library and Foundation. readLine() waits for you to type in the terminal while the program runs.

Try it above

How Swift runs here

Versions
Swift 6.4 on Linux
Build
swiftc -o a.out *.swift
Run
./a.out
Limits per run
512 MB memory, 30 s of CPU time on half a core, 2 MB of output
Time
Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
Sandbox
Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
Good to know
Foundation is available; SwiftUI and UIKit are not (they need Apple platforms).

A Swift program that reads input

This main.swift reads a few items into structs, skips lines it can’t parse, and prints the totals with String(format:) from Foundation:

Swift
import Foundation

struct Item {
    let name: String
    let price: Double
    let quantity: Int
    var total: Double { price * Double(quantity) }
}

print("How many items? ", terminator: "")
let count = Int(readLine() ?? "") ?? 0

var cart: [Item] = []
for i in 0..<count {
    print("Item \(i + 1) (name price qty): ", terminator: "")
    let parts = (readLine() ?? "").split(separator: " ")
    guard parts.count == 3, let price = Double(parts[1]), let qty = Int(parts[2]) else {
        print("Skipped: type a name, a price and a quantity")
        continue
    }
    cart.append(Item(name: String(parts[0]), price: price, quantity: qty))
}

for item in cart.sorted(by: { $0.total > $1.total }) {
    print("\(item.name): \(String(format: "%.2f", item.total))")
}
let sum = cart.reduce(0.0) { $0 + $1.total }
print("Total: \(String(format: "%.2f", sum))")

Three items typed in, printed with the biggest total first:

Terminal
$ swiftc -o a.out *.swift
$ ./a.out
How many items? 3
Item 1 (name price qty): coffee 3.50 2
Item 2 (name price qty): bagel 2.25 3
Item 3 (name price qty): juice 4.10 1
coffee: 7.00
bagel: 6.75
juice: 4.10
Total: 17.85

Reading input in Swift

readLine() returns the next line without its newline, as a String? (an optional). Converting is optional too: Int("42") gives an Int?, because the text might not be a number. So reading a number means unwrapping twice.

  • Int(readLine()!)! works on good input and crashes on anything else. Prefer if let or guard let, or a default with ??, as in the example.
  • Int(" 42") and Int("42 ") are nil: the conversion doesn’t trim spaces. For several values on one line, split(separator: " ") and convert each part.
  • A prompt printed with terminator: "" shows before the input here, since the program’s output is unbuffered.
  • while let line = readLine() loops until the end of input, which this terminal never sends (there’s no Ctrl+D). Read a count first, or stop on an empty line.

Several files

The build command is swiftc *.swift, so every .swift file in the playground is compiled into one program. Add files with the + next to the tabs; types and functions in them are visible everywhere without imports. Only main.swift may contain top-level statements:

Swift
// Shapes.swift: declarations only
protocol Shape { var area: Double { get } }
struct Circle: Shape { let r: Double; var area: Double { Double.pi * r * r } }
struct Square: Shape { let side: Double; var area: Double { side * side } }

// main.swift: the only file with top-level statements
let shapes: [any Shape] = [Circle(r: 1), Square(side: 2)]
for s in shapes { print(s.area) }

What isn’t available here

  • SwiftUI, UIKit, AppKit and Combine. They exist only on Apple platforms, so import SwiftUI fails on Linux.
  • Swift packages. There’s no Package.swift and no network, so dependencies such as Alamofire or swift-argument-parser can’t be added.
  • Darwin. On Linux the C library module is Glibc, so import Darwin doesn’t compile. Foundation covers most of what you’d use it for.
  • Playgrounds-style live results and Xcode previews. Output is what your program prints.

Errors you’ll see

  • “error: no such module 'SwiftUI'” An Apple-only framework. Write a command-line program that prints its results.
  • “error: cannot find 'total' in scope” A typo, or a variable declared inside a block you’ve left.
  • “error: value of optional type 'String?' must be unwrapped to a value of type 'String'” readLine() and dictionary lookups return optionals. Use if let, guard let or ??.
  • “error: cannot assign to value: 'count' is a 'let' constant” Declare it with var if it changes.
  • “Fatal error: Unexpectedly found nil while unwrapping an Optional value” A ! met nil, often Int(readLine()!)! with input that wasn’t a number.
  • “Fatal error: Index out of range” An array index past the end. Check indices with array.indices.contains(i).

Fix with AI under the failed run can turn a chain of ! unwraps into guard let checks, shown as a diff.

Swift 6.4 on Linux in practice

  • Foundation on Linux covers what command-line programs use: String(format:), Date, DateFormatter, JSONEncoder and JSONDecoder.
  • Builds aren’t optimized (no -O), so heavy loops run slower than a release build would. Array bounds and integer overflow are checked at run time and stop the program.
  • Structs and enums with associated values plus switch cover most exercises; switch must be exhaustive, which the compiler checks for you.
  • if and switch work as expressions: let grade = if score >= 90 { "A" } else { "B" }.

Questions

Which Swift version does this use?

Swift 6.4 on Linux, compiled with swiftc and run as a native binary.

Can I run SwiftUI or iOS code?

No. SwiftUI and UIKit need Apple platforms. You can run Swift that prints to the terminal, with Foundation.

How do I read an integer from input in Swift?

let n = Int(readLine() ?? "") ?? 0 reads a line and falls back to 0 if it isn’t a number. Type the value in the terminal while the program runs.

Is Foundation available?

Yes. import Foundation works and gives you String(format:), Date, JSONEncoder and the rest of the Linux Foundation.

Can I use Swift packages?

No. There’s no package manager here and runs have no network, so only the standard library and Foundation are available.