تعداد أساسي
enum Direction {
case up, down, left, right
}
let d = Direction.up
switch d {
case .up: print("للأعلى")
default: print("اتجاه آخر")
}
القيم الخام (Raw Values)
enum Planet: Int {
case mercury = 1, venus, earth // 1, 2, 3
}
print(Planet.earth.rawValue) // 3
let p = Planet(rawValue: 1) // Optional(.mercury)
القيم المرتبطة (Associated Values)
enum Result {
case success(String)
case failure(Int)
}
let r = Result.success("تمّ")
switch r {
case .success(let msg): print(msg)
case .failure(let code): print("خطأ \(code)")
}
دوال داخل التعداد
enum Light {
case red, green
func action() -> String {
switch self {
case .red: return "قف"
case .green: return "سر"
}
}
}
🎯 التالي: البروتوكولات.