Swift for Lua Developers
Move from Lua to Swift through static types, value semantics, optionals, Unicode, protocols and ARC. Complete 48 multi-file exercises, build a parcel desk and port two complete Lua programs.
Lua and Swift side by side
The same program in Lua and in Swift, from the lessons of this course.
Bindings and Mutation
Lua values have types, but an ordinary local variable can be rebound from a string to a number. Copying a string into another local does not make that local follow later reassignments. Lua strings are immutable, while tables introduce a different sharing…
Lua1local label = "draft"2local saved = label3label = 174print(saved .. " " .. label)Swift1let saved = "draft"2var label = saved3label = "ready"4print(saved, label)Strings and Interpolation
Lua builds formatted strings with string.format or concatenation using the two-dot operator. The formatting call here uses a numeric placeholder and %% for a literal percent sign. Standard Lua has no built-in interpolation syntax that evaluates expressions…
Lua1local done = 32print(string.format("progress=%d%%", done * 25))3print(string.sub("report.csv", 1, 6) == "report")Swift1let done = 32print("progress=\(done * 25)%")3print("report.csv".hasPrefix("report"))Switch and Patterns
Standard Lua has no switch statement or pattern-matching syntax. An if/elseif chain or a dispatch table can route on a value. Here two explicit comparisons accept pause and stop; writing command == "pause" or "stop" would instead make the second operand an…
Lua1local command = "pause"2if command == "pause" or command == "stop" then3 print("idle")4else5 print("running")6endSwift1let command = "pause"2switch command {3case "pause", "stop": print("idle")4default: print("running")5}Functions and Labels
Lua calls normally pass positional arguments. A table argument can simulate named options, but it is a table value interpreted by the function, not a language-level argument-label declaration. Missing positional arguments become nil, and extra arguments may…
Lua1local function echo(sound, times)2 return string.rep(sound, times)3end4print(echo("ha", 3))Swift1func echo(_ sound: String, times count: Int) -> String {2 return String(repeating: sound, count: count)3}Dictionaries and Missing Keys
Lua uses tables for both sequences and key-value maps. Setting a field to nil removes it. The # operator describes a sequence border and cannot count arbitrary string-key entries, so this example counts with pairs. Enumeration order from pairs is not a sorted…
Lua1local settings = {font = 14, gap = 2}2settings.gap = nil3print(settings.gap or 8)4local count = 05for _ in pairs(settings) do count = count + 1 end6print(count)Swift1var settings = ["font": 14, "gap": 2]2settings["gap"] = nil3print(settings["gap"] ?? 8)4print(settings.count)Structs and Independent Copies
A Lua record is commonly a table with named fields. Assigning that table to another local copies its reference, not its fields. Mutating direction through either name changes the same record; even a local binding convention does not turn that table into an…
Lua1local first = {direction = "north"}2local second = first3second.direction = "west"4print(first.direction .. " " .. second.direction)Swift1struct Heading {2 var direction: String3 mutating func turnWest() { direction = "west" }4}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsLua Ports
2 lessonsFrequently asked questions
Who is Swift for Lua Developers for?
For people who already write Lua and want to learn Swift. Each lesson starts from Lua you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need Lua basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for Lua Developers have?
Swift for Lua Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for Lua Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for Lua Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.