0
0
Swiftprogramming~20 mins

Raw values for enums in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Raw Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum raw value access
What is the output of this Swift code snippet?
Swift
enum Direction: String {
    case north = "N"
    case south = "S"
    case east = "E"
    case west = "W"
}

let dir = Direction.east
print(dir.rawValue)
ADirection.east
B"east"
C"E"
D"East"
Attempts:
2 left
💡 Hint
Check the raw value assigned to the case east.
Predict Output
intermediate
2:00remaining
Raw value default assignment for Int enums
What will be printed by this Swift code?
Swift
enum Weekday: Int {
    case monday = 1
    case tuesday
    case wednesday
}

print(Weekday.tuesday.rawValue)
A3
B1
Ctuesday
D2
Attempts:
2 left
💡 Hint
Int raw values auto-increment from the first assigned value.
Predict Output
advanced
2:30remaining
Using rawValue initializer with valid and invalid values
What is the output of this Swift code?
Swift
enum StatusCode: Int {
    case success = 200
    case notFound = 404
    case serverError = 500
}

if let status = StatusCode(rawValue: 404) {
    print("Found: \(status)")
} else {
    print("Not found")
}

if let status = StatusCode(rawValue: 201) {
    print("Found: \(status)")
} else {
    print("Not found")
}
A
Found: notFound
Not found
B
Found: notFound
Found: 201
C
Not found
Not found
D
Found: 404
Not found
Attempts:
2 left
💡 Hint
rawValue initializer returns nil if no matching case exists.
Predict Output
advanced
3:00remaining
Raw values with mixed types in enum
What error or output does this Swift code produce?
Swift
enum MixedRaw: RawRepresentable {
    case intCase(Int)
    case stringCase(String)

    var rawValue: Any {
        switch self {
        case .intCase(let value): return value
        case .stringCase(let value): return value
        }
    }

    init?(rawValue: Any) {
        if let intValue = rawValue as? Int {
            self = .intCase(intValue)
        } else if let stringValue = rawValue as? String {
            self = .stringCase(stringValue)
        } else {
            return nil
        }
    }
}

let a = MixedRaw(rawValue: 10)
print(a?.rawValue ?? "nil")
Anil
BCompile-time error: enums cannot have associated values and raw values
C10
DRuntime error
Attempts:
2 left
💡 Hint
Swift enums with associated values cannot have raw values.
🧠 Conceptual
expert
2:30remaining
Raw value uniqueness enforcement
What happens if you define a Swift enum with duplicate raw values like this?
Swift
enum Color: String {
    case red = "R"
    case green = "G"
    case blue = "R"
}
ACompile-time error due to duplicate raw values
BCompiles successfully, last case overwrites previous raw value
CRuntime error when accessing rawValue
DCompiles but rawValue of blue is nil
Attempts:
2 left
💡 Hint
Raw values must be unique in Swift enums.