Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created November 7, 2024 14:11
Show Gist options
  • Save lucianoschillagi/b569780b5559162d03833d512a9da5fc to your computer and use it in GitHub Desktop.
Save lucianoschillagi/b569780b5559162d03833d512a9da5fc to your computer and use it in GitHub Desktop.
Enumerations with Associated Values
import Foundation
// Enumerations with Associated Values
enum OrderStatus {
case pending(orderID: Int, isAnOffer: Bool)
case shipped(orderID: Int, trackingNumber: String, isAnOffer: Bool)
case delivered(orderID: Int, date: String, signature: String?, isAnOffer: Bool)
}
// Usage examples
let order1 = OrderStatus.pending(orderID: 101, isAnOffer: false)
let order2 = OrderStatus.shipped(orderID: 102, trackingNumber: "1Z999AA10123456784", isAnOffer: true)
let order3 = OrderStatus.delivered(orderID: 103, date: "2024-11-03", signature: nil, isAnOffer: true)
dump(order1)
dump(order2)
dump(order3)
// https://dc4p.store/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment