Last active
May 16, 2016 03:48
-
-
Save matux/1b78defe94bae21a0392 to your computer and use it in GitHub Desktop.
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
| import UIKit | |
| import Aspects | |
| // MARK: Infix Segue operators | |
| infix operator » {} | |
| func » <U: SegueDestination, T: UIViewController where T: Segueable, T.Destination == U> | |
| (sourceViewController: T, destination: U) | |
| { | |
| sourceViewController.segue(to: destination, prepare: nil) | |
| } | |
| // MARK: - SegueDestination class definition | |
| class SegueDestination { | |
| let identifier: String | |
| init(_ identifier: String) { | |
| self.identifier = identifier | |
| } | |
| } | |
| // MARK: - Segueable protocol definition | |
| typealias SeguePreparationClosure = (segue: UIStoryboardSegue, sender: AnyObject?) -> Void | |
| protocol Segueable: class { | |
| typealias Destination | |
| func segue(to destination: Destination, prepare: SeguePreparationClosure?) | |
| } | |
| extension Segueable where Self: UIViewController, Destination: SegueDestination { | |
| func segue(to destination: Destination, prepare: SeguePreparationClosure? = nil) { | |
| self.performSegue(withIdentifier: destination.identifier) | |
| } | |
| } | |
| // MARK: - Perform Segue UIViewController super controversial hook | |
| extension UIViewController { | |
| func performSegue(segueIdentifier: String, prepare: SeguePreparationClosure? = nil) { | |
| defer { self.performSegueWithIdentifier(segueIdentifier, sender: self) } | |
| guard let preparationClosure = prepare else { return } | |
| let hook: @convention(block)(AnyObject, UIStoryboardSegue, AnyObject?) -> Void = { _, segue, sender in | |
| preparationClosure(segue: segue, sender: sender) | |
| } | |
| do { | |
| let options: AspectOptions = [.PositionAfter, .OptionAutomaticRemoval] | |
| let block = unsafeBitCast(hook, AnyObject.self) | |
| try self.aspect_hookSelector("prepareForSegue:sender:", withOptions: options, usingBlock: block) | |
| } catch let error as NSError | |
| where error.code == Int(AspectErrorCode.SelectorAlreadyHookedInClassHierarchy.rawValue) | |
| { | |
| assert(false, "This shouldn't happen since we're not swizzling the metaclass.") | |
| } catch { } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment