Skip to content
Justice Tech School

Enums

Enums define a set of cases that can carry values and methods.

Example
enum Level: Int { case low = 1, mid, high }
print(Level.mid.rawValue, Level(rawValue: 3)!)
enum Result { case ok(Int), failure(String) }
let r = Result.ok(5)
switch r {
case .ok(let v): print("ok", v)
case .failure(let m): print(m)
}

Exercise

Associated value.

case ok()

Quiz Test

What are associated values?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.