Swift playground

Run Swift online

Write, compile and run Swift online with Swift 6. A free Swift compiler in your browser, with Foundation, readLine() input and compiler errors. No sign-up.

Code main.swift

// Welcome to the coding.kitty Swift playground.
// Press Run (or Ctrl+Enter) to compile this and see what it prints.

struct Cat {
    let name: String
    let age: Int

    func describe() -> String {
        "\(name) is \(age) and says meow"
    }
}

let cats = [
    Cat(name: "Miko", age: 3),
    Cat(name: "Nola", age: 5),
    Cat(name: "Baymax", age: 1),
]

// Youngest first.
for (i, cat) in cats.sorted(by: { $0.age < $1.age }).enumerated() {
    print("\(i + 1). \(cat.describe())")
}

// Type a name in the Input box and press Run again to see this line.
if let name = readLine(), !name.isEmpty {
    print("Hello, \(name)!")
}
Output

Press Run to see what your code prints.

About this Swift playground

This is a Swift compiler you can use straight away. Write Swift in the editor, press Run, and your program is compiled with swiftc and run. The output appears next to it: everything it prints, the compiler's errors and warnings with their line numbers, any crash with its message, and how long it took.

It compiles with Swift 6.4 on Linux, in an isolated sandbox on our servers, and your file is main.swift, so a program can be as short as a single print. It is free, with nothing to install, no Mac and no account needed.

What you can run

  • Swift 6.4, running one file per run, saved as main.swift.
  • Compiled, then run. Each run compiles your file with swiftc -enable-bare-slash-regex -o main main.swift and runs the program if it compiles. Compiler errors and warnings appear in the output like any other error.
  • Standard library only. The Swift standard library (arrays, dictionaries, sets, strings, optionals, Codable, regex and async and await) and Foundation, so JSONEncoder, Date, DateFormatter, String(format:) and the rest of Foundation's everyday types import as normal. There is no package installer, and programs have no network access.
  • Limits: 10 seconds and 256 MB of memory per run, plus up to 20 seconds to compile first. Output past 64 KB is cut off, code can be up to 64 KB and input up to 16 KB.
  • Fair use: 5 runs a minute and 50 a day without an account, 10 a minute when you are signed in.

Reading input

Open the Input box under the editor and type what your program should read. readLine() reads it a line at a time, without the newline, and returns nil once there is nothing left to read, straight away when the box is empty.

let age = Int(readLine() ?? "") ?? 0

Frequently asked questions

Is this online Swift compiler free?

Yes. There is no sign-up and no limit on how much you can write. Signed-out visitors get a quick, usually invisible bot check when they press Run, and a fair-use cap of 5 runs a minute and 50 a day. Signing in raises it to 10 runs a minute.

Which version of Swift does it use?

Swift 6.4 on Linux, compiling in Swift 5 language mode, the compiler's default, so top-level code can use global variables without Swift 6's strict concurrency errors. Structs, enums with associated values, protocols, generics, async and await, actors and regex literals all work.

Can I use SwiftUI, UIKit or Swift packages?

No. The playground compiles one file on Linux, so Apple's frameworks such as SwiftUI and UIKit are not there, and with no Swift Package Manager and no network, neither are packages. The standard library and Foundation are, but not URLSession, since programs cannot reach the internet.

Can I use @main?

Not here. Your file is compiled as main.swift, whose top-level code is the program, and Swift does not allow a @main type in a file with top-level code. Write your statements at the top level, as the example does, and call your own functions from there.

Why does compiling take a moment?

swiftc type-checks and builds your whole program before it runs, which takes about a second for a small one. Compiling has 20 seconds of its own, separate from your program's time, so a slow compile never cuts your program short.

Why did my program stop with a time-out?

Compiling gets up to 20 seconds of its own. Then the program gets 10 seconds and 256 MB of memory, and is stopped if it goes past either. The usual cause is a loop that never ends, such as a while true loop without a break.

Is my code saved or shared?

Your code is kept in your own browser so it is still there next time, and nowhere else. When you press Run it is sent to our code runner, run once in a sandbox, and thrown away. We do not store it or show it to anyone.

Keep going

Also in the playground: Python, JavaScript, TypeScript, SQL, Bash, C, C++, Go, Rust, Java, C#, Ruby, PHP, Lua, R, Dart.