使用Swift编程,未免会使用到Runtime,下面是Swift-RunTime的基本使用,希望对大家有所帮助。
1. Runtime扩展属性
以UIView扩展 loadingView
和 blankPageView
属性为例
private struct AssociatedKeys{
static var loadingViewKey:LGLoadingView?
static var blankPageViewKey:LGBlankView?
}
var loadingView: LGLoadingView? {
get { return objc_getAssociatedObject(self, &AssociatedKeys.loadingViewKey) as? LGLoadingView }
set {
if let newValue = newValue {
objc_setAssociatedObject(self, &AssociatedKeys.loadingViewKey, newValue,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
var blankPageView: LGBlankView? {
get { return objc_getAssociatedObject(self, &AssociatedKeys.blankPageViewKey) as? LGBlankView }
set {
if let newValue = newValue {
objc_setAssociatedObject(self, &AssociatedKeys.blankPageViewKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
2. Runtime Swizzle方法
以交换UIViewController的生命周期方法为例
extension UIViewController{
public override static func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// 确保不是子类
if self !== UIViewController.self {
return
}
dispatch_once(&Static.token) {
mySwizzle(self, origSEL: #selector(UIViewController.viewWillAppear), newSEL: #selector(UIViewController.customViewWillAppear))
mySwizzle(self, origSEL: #selector(UIViewController.viewWillDisappear), newSEL: #selector(UIViewController.customViewWillDisappear))
mySwizzle(self, origSEL: #selector(UIViewController.viewDidAppear), newSEL: #selector(UIViewController.customViewDidAppear))
}
}
func customViewWillAppear(animated: Bool) {
self.customViewWillAppear(animated)
}
func customViewWillDisappear(animated: Bool){
if self.navigationItem.backBarButtonItem == nil
&& self.navigationController?.viewControllers.count > 1 {
self.navigationItem.backBarButtonItem = self.backButton()
}
self.customViewWillDisappear(animated)
}
func customViewDidAppear(animated: Bool) {
self.customViewDidAppear(animated)
}
//交换方法
class func mySwizzle(cls:AnyClass,origSEL:Selector,newSEL:Selector){
let originalMethod = class_getInstanceMethod(cls, origSEL)
let swizzledMethod = class_getInstanceMethod(cls, newSEL)
let didAddMethod = class_addMethod(cls, origSEL, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(cls, newSEL, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
//返回按钮
func backButton() -> UIBarButtonItem {
let temporaryBarButtonItem = UIBarButtonItem()
temporaryBarButtonItem.title = "返回"
temporaryBarButtonItem.target = self
if temporaryBarButtonItem.respondsToSelector(#selector(UIBarItem.setTitleTextAttributes(_:forState:))) {
let textAttributes = [NSFontAttributeName:UIFont.boldSystemFontOfSize(17),
NSForegroundColorAttributeName:UIColor.whiteColor()
]
UIBarButtonItem.appearance().setTitleTextAttributes(textAttributes, forState: .Normal)
}
temporaryBarButtonItem.action = #selector(UIViewController.goBack)
return temporaryBarButtonItem;
}
func goBack() {
self.navigationController?.popViewControllerAnimated(true)
}
}
3.RunTime 获取对象的所有属性名和属性值和获取对象的所有方法名
func allPropertyNamesAndValues() ->[String: AnyObject] {
var count: UInt32 = 0
let properties = class_copyPropertyList(Person.self, &count)
var resultDict: [String: AnyObject] = [:]
for var i = 0; i < Int(count); ++i {
let property = properties[i]
// 取得属性名
let name = property_getName(property)
if let propertyName = String.fromCString(name) {
// 取得属性值
if let propertyValue = self.valueForKey(propertyName) {
resultDict[propertyName] = propertyValue
}
}
}
return resultDict
}
func allMethods() {
var count: UInt32 = 0
let methods = class_copyMethodList(Person.self, &count)
for var i = 0; i < Int(count); ++i {
let method = methods[i]
let sel = method_getName(method)
let methodName = sel_getName(sel)
let argument = method_getNumberOfArguments(method)
print("name: (methodName), arguemtns: (argument)")
}
}
后语:保剑锋从磨砺出,梅花香自苦寒来。。。