Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created July 12, 2016 06:32
Show Gist options
  • Save jweinst1/39cb1df905611e8a23991c0545271b1e to your computer and use it in GitHub Desktop.
Save jweinst1/39cb1df905611e8a23991c0545271b1e to your computer and use it in GitHub Desktop.
using advanced applications of enums in swift
//playground for experimenting with enums
enum pair {
case First(Int)
case Second(Int)
//add to the first case
mutating func addToFirst(amount:Int) {
switch(self){
case .First(let current):
self = .First(current + amount)
default: break
}
}
//returns the value of the enum
func getValue() -> Int {
switch(self){
case .First(let current):
return current
case .Second(let current):
return current
}
}
//computed property
var string: String {
switch(self){
case .First(let current): return String(current)
case .Second(let current): return String(current)
}
}
}
var f = pair.First(4)
f.addToFirst(4)
print(f.getValue())
f.string
//"8\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment