Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created December 16, 2015 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onmyway133/2bea2d4047bdbf8ab53b to your computer and use it in GitHub Desktop.
Save onmyway133/2bea2d4047bdbf8ab53b to your computer and use it in GitHub Desktop.
SwizzleInit.swift
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