Last active
August 29, 2015 14:08
-
-
Save BestKora/d3c38fc092a7336c6abd to your computer and use it in GitHub Desktop.
Swift: Follow Article "Swift: Failable Enums with Optionals" http://natashatherobot.com/swift-failable-enums-with-optionals/?utm_campaign=This_Week_in_Swift_14&utm_medium=email&utm_source=This%2BWeek%2Bin%2BSwift
This file contains 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
extension String { | |
func toEnum<Enum: RawRepresentable where Enum.RawValue == String>() -> Enum? { | |
return Enum(rawValue: self) | |
} | |
} | |
class ViewController: UIViewController { | |
............. | |
// Replace Magic Strings with Enumerations in Swift | |
enum SegueIdentifier: String { | |
case SegueToRedViewIdentifier = "SegueToRedViewIdentifier" | |
case SegueToGreenViewIdentifier = "SegueToGreenViewIdentifier" | |
case SegueToBlueViewIdentifier = "SegueToBlueViewIdentifier" | |
} | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
let y: SegueIdentifier? = segue.identifier?.toEnum() // IMPORTANT! | |
if let segueIdentifier = y { | |
switch segueIdentifier { | |
case .SegueToRedViewIdentifier: | |
println("red") | |
case .SegueToGreenViewIdentifier: | |
println("green") | |
case .SegueToBlueViewIdentifier: | |
println("blue") | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment