看了几天的Runtime ,教程还有简书的也大多都是OC的,结果要转成SWIFT的时候落进了一堆的大坑,还是专心看Swift就好吧 ...
WildDylan写的Runtime写的很清晰、很易入门:http://www.jianshu.com/p/9c36a5b7820a
在大神的基础上,练练手,存在extension UIViewController了,
也当把代码存在简书上,以后方便调用!
//
// ViewController.swift
// SwizzlingTestSwift
//
// Created by Linus on 16/3/25.
// Copyright © 2016年 Linus. All rights reserved.
//
import UIKit
class TestSwiftClass {
@objc var sex:Bool = true
@objc var age:Int = 22
@objc var name:String = "Dylin"
@objc var cars:AnyObject! = nil
}
class ViewController: UIViewController {
/*
*
http://www.163.com/中文 为 nil
http://www.163.com/ 有值
*
*/
var sex:Bool = true
var age:Int = 22
var name:String = "Dylin"
var cars:AnyObject! = nil
override func viewDidLoad() {
super.viewDidLoad()
// 调用extension UIViewController里的函数,已黑魔法交换
debugPrint("==",returnInt(1))
}
// 没有在 func 前加 dynamic 是交换不了的
func returnTuple (_ name:String) -> (Bool,Int,String) {
return (true,22,"Dylin")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// extension.swift
//
// Created by apiapia on 3/16/17.
// Copyright © 2017 Linus. All rights reserved.
//
import Foundation
import UIKit
extension UIViewController{
// 初始化的时候就调用RunTime 黑魔法
open override static func initialize() {
super.initialize()
// 确保不是子类
if self !== UIViewController.self {
return
}
//黑魔法的使用
mySwizzle(cls: self, origSEL: #selector(UIViewController.viewWillAppear),
newSEL: #selector(UIViewController.customViewWillAppear))
mySwizzle(cls: self, origSEL: #selector(UIViewController.viewWillDisappear),
newSEL: #selector(UIViewController.customViewWillDisappear))
mySwizzle(cls: self, origSEL: #selector(UIViewController.viewDidAppear),
newSEL: #selector(UIViewController.customViewDidAppear))
mySwizzle(cls: self,
origSEL: #selector(UIViewController.returnInt(_ :)),
newSEL: #selector(UIViewController.new_returnInt(_ :)))
}
dynamic func returnInt (_ value:Int) -> Int {
print("before")
return 5
}
dynamic func new_returnInt (_ value:Int) -> Int {
print ("new")
return 10
}
func customViewWillAppear(animated: Bool) {
self.customViewWillAppear(animated: animated)
}
func customViewWillDisappear(animated: Bool){
self.customViewWillDisappear(animated: animated)
}
func customViewDidAppear(animated: Bool) {
self.customViewDidAppear(animated: animated)
print ("cusomViewDidAppear")
}
//交换方法函数
class func mySwizzle(cls:AnyClass,origSEL:Selector,newSEL:Selector){
//原有方法
let originalMethod:Method = class_getInstanceMethod(cls, origSEL)
//新方法(替换原有方法的新方法)
let swizzleMethod:Method = class_getInstanceMethod(cls, newSEL)
//先尝试給源SEL添加IMP,这里是为了避免源SEL没有实现IMP的情况
let didAddMethod = class_addMethod(cls, origSEL,
method_getImplementation(swizzleMethod),
method_getTypeEncoding(swizzleMethod))
if didAddMethod {
//添加成功:说明源SEL没有实现IMP,将源SEL的IMP替换到交换SEL的IMP
class_replaceMethod(cls, newSEL,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod))
}else {
//添加失败:说明源SEL已经有IMP,直接将两个SEL的IMP交换即可
method_exchangeImplementations(originalMethod, swizzleMethod)
}
}
}