Enums as Finite Choices
Lesson 12 of 48 in Coddy's Swift for C Developers course.
A C enum gives names to integer constants, with conversion and representation rules that do not create the same closed value model as Swift. Swift enums are value types with declared cases, and a switch over all cases can be exhaustive without default. Associated data is a separate feature introduced later.
The source uses a separate function to map orientation to a label. Swift attaches a computed property to the enum and switches on self. A leading dot names a case when its type is inferred. C numeric enumerator values should not be mistaken for implicit stable Swift serialization values.
C: complete program
orientation.h
#ifndef ORIENTATION_H
#define ORIENTATION_H
typedef enum { PORTRAIT, LANDSCAPE } Orientation;
static const char *orientation_label(Orientation value) {
switch (value) { case PORTRAIT: return "tall"; case LANDSCAPE: return "wide"; }
return "unknown";
}
#endif
main.c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "orientation.h"
int main(void) {
Orientation orientation = LANDSCAPE;
puts(orientation_label(orientation));
return 0;
}
Swift: complete program
Orientation.swift
import Foundation
enum Orientation {
case portrait, landscape
var label: String {
switch self {
case .portrait: return "tall"
case .landscape: return "wide"
}
}
}main.swift
import Foundation
let orientation: Orientation = .landscape
print(orientation.label)Use explicit raw values when a Swift enum needs a stored external representation. Declaration order alone is not a serialization contract.
Challenge
EasyImplement Signal.next() -> Signal and Signal.label: String in Signal.swift. Cases are red, green, amber. The cycle is red to green to amber to red, and label is the lowercase case name. Input is one case name; output the next label.
Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.
Your code goes in Signal.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
import Foundation
func apiChecks(_ input: String) -> Bool {
return Signal.red.next() == .green && Signal.green.next().next() == .red && Signal.amber.label == "amber"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for C Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry Rules3Control Flow
Switch and PatternsEnums as Finite ChoicesFor Loops and RangesWhile and Repeat LoopsRecap: Sequence ScanPractice on your own: Swift playground