Menu
CoddyTech
Course

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.

  1. 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…

    Lua
    1local label = "draft"2local saved = label3label = 174print(saved .. " " .. label)
    Swift
    1let saved = "draft"2var label = saved3label = "ready"4print(saved, label)
  2. 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…

    Lua
    1local done = 32print(string.format("progress=%d%%", done * 25))3print(string.sub("report.csv", 1, 6) == "report")
    Swift
    1let done = 32print("progress=\(done * 25)%")3print("report.csv".hasPrefix("report"))
  3. 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…

    Lua
    1local command = "pause"2if command == "pause" or command == "stop" then3  print("idle")4else5  print("running")6end
    Swift
    1let command = "pause"2switch command {3case "pause", "stop": print("idle")4default: print("running")5}
  4. 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…

    Lua
    1local function echo(sound, times)2  return string.rep(sound, times)3end4print(echo("ha", 3))
    Swift
    1func echo(_ sound: String, times count: Int) -> String {2    return String(repeating: sound, count: count)3}
  5. 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…

    Lua
    1local settings = {font = 14, gap = 2}2settings.gap = nil3print(settings.gap or 8)4local count = 05for _ in pairs(settings) do count = count + 1 end6print(count)
    Swift
    1var settings = ["font": 14, "gap": 2]2settings["gap"] = nil3print(settings["gap"] ?? 8)4print(settings.count)
  6. 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…

    Lua
    1local first = {direction = "north"}2local second = first3second.direction = "west"4print(first.direction .. " " .. second.direction)
    Swift
    1struct Heading {2    var direction: String3    mutating func turnWest() { direction = "west" }4}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for Lua developers

Learn Swift from another language

Frequently 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.

Coddy programming languages illustration

Start Swift for Lua Developers for free

Get started