Swift Files
Write a small receipt only inside a newly owned temporary directory.
How it works
Foundation supplies file and data operations across supported platforms. Keep the directory’s ownership explicit, use a chosen text encoding and close handles before removing files. A UUID-based learning folder is separate from a real project and must never be replaced by an arbitrary user-supplied deletion target.
Run this Swift example
Install the toolchain for your platform from Swift.org, then check swift --version. The reviewed portable baseline is Swift 6.4.2 in Swift 6 mode on Ubuntu 26.04 x86_64 through the PC’s existing WSL installation. These are real Linux executions, not claims of Windows-native or Apple-platform test results. Windows Swift has separate C++/SDK prerequisites in its official setup guide.
Save the complete source as files.swift in a new learning folder. On Linux/macOS compile with swiftc -swift-version 6 -parse-as-library files.swift -o lesson, then run ./lesson. On a configured Windows toolchain use an output such as lesson.exe and run .\lesson.exe. The parse-as-library option supports the explicit @main entry point used by these sources. Compile each ordinary lesson separately.
Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.
Download the complete files.swift · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.
Example
import Foundation
@main
struct Lesson {
static func main() {
let manager = FileManager.default
let directory = manager.temporaryDirectory.appendingPathComponent("bhylu-swift-" + UUID().uuidString, isDirectory: true)
do {
guard !manager.fileExists(atPath: directory.path) else { fatalError("Existing output refused") }
try manager.createDirectory(at: directory, withIntermediateDirectories: false)
defer {
do { try manager.removeItem(at: directory) }
catch { fatalError("Owned cleanup failed") }
}
let file = directory.appendingPathComponent("receipt.txt")
try Data("P-31 collected\n".utf8).write(to: file, options: .withoutOverwriting)
print(try String(contentsOf: file, encoding: .utf8), terminator: "")
} catch { fatalError("Owned file example failed: \(error)") }
}
}
Expected result
P-31 collected
The only cleanup path is the directory this successful run created. The write refuses an existing receipt. This is a small trusted fixture, not a full adversarial path-containment, backup or encrypted-storage implementation.
Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.