Menu
CoddyTech
Course

Lua for Swift Developers

Transfer Swift experience to Lua 5.1: truth rules, tables, functions, module exports, errors and metatable objects. Build a stock ledger and port two Swift reference programs.

Swift and Lua side by side

The same program in Swift and in Lua, from the lessons of this course.

  1. Print and Comment

    Swift print accepts a separator and terminator and interpolation converts values into text. Lua print inserts tabs between arguments and a final newline. Build one string with .. when exact formatting matters, and return it to the locked driver. Lua comments…

    Swift
    1let name = "Ada"2print("Hi, \(name)")
    Lua
    1print("Hi" .. ", Ada")
  2. Nil and Missing Fields

    Swift Optional represents a missing value with nil and distinguishes it from false. Dictionary lookup adds an optional layer, and assigning nil through a dictionary subscript removes its entry. Lua nil likewise removes a table association, but Lua has no…

    Swift
    1var values = ["x": 1]2values["x"] = nil3print(values.count)
    Lua
    1local d = {x = nil}2print(d.x == nil)
  3. Ranges to Numeric for

    Swift half-open ranges exclude their upper bound and closed ranges include it. Lua numeric for always includes its limit and accepts an optional step. Translate the values actually visited, and use a negative step for descending traversal. Keep empty-range…

    Swift
    1var total = 02for i in 1..<4 { total += i }3print(total)
    Lua
    1local total = 02for i = 1, 3 do total = total + i end3print(total)
  4. Functions and Returns

    Swift function signatures declare parameter and return types; inout explicitly permits writeback to caller storage. Lua parameters have no declared types. Rebinding a numeric parameter affects only its local name, so return and assign an updated scalar.…

    Swift
    1func twice(_ n: Int) -> Int { n * 2 }2print(twice(4))
    Lua
    1local function twice(x) return x * 2 end2print(twice(4))
  5. Lists as Dense Tables

    Swift Array is a value type with zero-based indexing and copy-on-write storage. Lua represents dense lists with one-based table keys. table.insert and table.remove shift sequence elements; assigning nil leaves a hole instead. The length operator is reliable…

    Swift
    1var values = [2, 3]2values.append(4)3print(values.count)
    Lua
    1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))
  6. Sorting and Comparators

    Swift sort mutates a collection and sorted returns a new collection; both accept a Boolean ordering predicate. Lua table.sort mutates the sequence and returns no value. Its comparator must report strict ordering. Keep the original table reference, and add a…

    Swift
    1var values = [10, 2, 1]2values.sort(by: <)3print(values.map(String.init).joined(separator: ","))
    Lua
    1local a={3,1,2}2table.sort(a)3print(a[1])

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

Final Source Ports

2 lessons

More courses for Swift developers

Learn Lua from another language

Frequently asked questions

Who is Lua for Swift Developers for?

For people who already write Swift and want to learn Lua. Each lesson starts from Swift you know and shows how Lua does the same thing, so you skip the beginner basics.

Do I need to know Lua already?

No. The course starts with your first Lua program. You need Swift basics: variables, conditions, loops, functions and collections.

How many lessons does Lua for Swift Developers have?

Lua for Swift Developers contains 48 interactive lessons you can complete at your own pace.

What does Lua for Swift Developers cover?

The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.

Is Lua for Swift 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 Lua for Swift Developers for free

Get started