Created
December 16, 2015 16:52
-
-
Save onmyway133/2bea2d4047bdbf8ab53b to your computer and use it in GitHub Desktop.
SwizzleInit.swift
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
import Foundation | |
import UIKit | |
extension UIStoryboardSegue { | |
public override class func initialize() { | |
struct Static { | |
static var token: dispatch_once_t = 0 | |
} | |
// Make sure this isn't a subclass | |
if self !== UIStoryboardSegue.self { | |
return | |
} | |
dispatch_once(&Static.token) { | |
let originalSelector = Selector("init:source:destination:") | |
let swizzledSelector = Selector("ftg_init:source:destination:") | |
let originalMethod = class_getClassMethod(self, originalSelector) | |
let swizzledMethod = class_getClassMethod(self, swizzledSelector) | |
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) | |
if didAddMethod { | |
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
} | |
} | |
// MARK: - Method Swizzling | |
class func ftg_init(identifier identifier: String!, | |
source: UIViewController, | |
destination: UIViewController) -> UIStoryboardSegue { | |
return ftg_init(identifier: identifier, | |
source: source, | |
destination: destination.ftg_resolve()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment