Skip to content

Instantly share code, notes, and snippets.

@bmoliveira
Created May 10, 2016 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoliveira/b932c659312bba4b6080e39653d827f1 to your computer and use it in GitHub Desktop.
Save bmoliveira/b932c659312bba4b6080e39653d827f1 to your computer and use it in GitHub Desktop.
protocol SegueHandlerType {
associatedtype SegueIdentifier: RawRepresentable
}
// notice the cool use of where here to narrow down
// what could use these methods.
extension SegueHandlerType where Self: UIViewController,
SegueIdentifier.RawValue == String
{
func performSegueWithIdentifier(segueIdentifier: SegueIdentifier,
sender: AnyObject?) {
performSegueWithIdentifier(segueIdentifier.rawValue, sender: sender)
}
func segueIdentifierForSegue(segue: UIStoryboardSegue) -> SegueIdentifier {
// still have to use guard stuff here, but at least you're
// extracting it this time
guard let identifier = segue.identifier,
segueIdentifier = SegueIdentifier(rawValue: identifier) else {
fatalError("Invalid segue identifier \(segue.identifier).") }
return segueIdentifier
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment