Created
July 12, 2016 06:32
-
-
Save jweinst1/39cb1df905611e8a23991c0545271b1e to your computer and use it in GitHub Desktop.
using advanced applications of enums in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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