Skip to content

Instantly share code, notes, and snippets.

@Kilo-Loco
Created June 5, 2024 16:36
Show Gist options
  • Save Kilo-Loco/06c3c658d555a4727f005ce976564f35 to your computer and use it in GitHub Desktop.
Save Kilo-Loco/06c3c658d555a4727f005ce976564f35 to your computer and use it in GitHub Desktop.
Examples of Enums in Swift
import SwiftUI
enum Color: Int {
case red
case blue
case yellow
}
enum Decision {
case yes
case no
}
enum SheetVisibilityState {
case showing
case hidden
}
enum ElementType: String {
case isScrollView
case colorContrast
}
enum LoginAPI {
case login(username: String, password: String)
}
struct ContentView: View {
@State var color: Color = .blue
@State var sheetVisibilityState = SheetVisibilityState.hidden
@State var elementType = ElementType.colorContrast
@State var api: LoginAPI?
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
func dosomething() {
switch elementType {
case .isScrollView:
print("logic for isScrollView")
case .colorContrast:
print("logic for colorContrast")
}
if elementType == .colorContrast {
}
switch api {
case .login(let name, let password):
print("Hello \(name), your password is \(password)")
case nil:
print("Case nil here because the api property is optional")
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment