Advertisement

Online Swift Compiler

Write, run and test Swift code online, right in your browser. This free online Swift compiler needs no installation or signup — just type your code and click Run.

Swift is Apple's fast, safe and expressive language for building iOS, macOS, watchOS and tvOS apps.

How to use the Swift compiler

  1. Write or paste your Swift code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in Swift

let name = readLine() ?? ""
print("Hello, \(name)!")

Paste this into the editor, type your name in the STDIN box and click Run.

More Swift examples to try

Optionals and guard

Safely reads a number from input with guard let.

guard let line = readLine(), let n = Int(line) else {
    print("Please enter a whole number in STDIN")
    exit(0)
}
print("\(n) squared is \(n * n)")

Structs and protocols

A protocol defines a requirement that structs adopt.

protocol Shape {
    func area() -> Double
}

struct Circle: Shape {
    let r: Double
    func area() -> Double { Double.pi * r * r }
}

struct Square: Shape {
    let side: Double
    func area() -> Double { side * side }
}

let shapes: [Shape] = [Circle(r: 2), Square(side: 3)]
for s in shapes {
    print(String(format: "%.2f", s.area()))
}

Common Swift errors and how to fix them

Fatal error: Unexpectedly found nil while unwrapping an Optional value
You force-unwrapped (!) a nil value — often missing input. Use if let, guard let or ??.
cannot assign to value: 'x' is a 'let' constant
Declare the value with var if it needs to change.
binary operator '+' cannot be applied to operands of type 'Int' and 'Double'
Swift does not mix number types automatically. Convert one, e.g. Double(x) + y.
cannot find 'x' in scope
The name is misspelled or declared inside another block.

Frequently asked questions

Is this online Swift compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my Swift program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning Swift?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.