Swift-Runtime 基本应用

作者: DevinWu | 来源:发表于2016-09-01 17:24 被阅读706次

使用Swift编程,未免会使用到Runtime,下面是Swift-RunTime的基本使用,希望对大家有所帮助。

1. Runtime扩展属性

以UIView扩展 loadingViewblankPageView 属性为例
  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)")
  }
}
后语:保剑锋从磨砺出,梅花香自苦寒来。。。

相关文章

  • Swift-Runtime 基本应用

    使用Swift编程,未免会使用到Runtime,下面是Swift-RunTime的基本使用,希望对大家有所帮助。 ...

  • Swift-Runtime机制

    相对于Objective-C的Runtime机制,Swift的运行时机制相对低调很多,Swift语言是用C++编写...

  • 1、Selenium -- 环境配置与基本用法

    概要: 应用场景 环境配置 基本应用基本定位js定位鼠标事件屏幕截图 一、应用场景 爬虫; 绕过登录(cookie...

  • MapKit使用

    MapKit基本应用

  • iOS MPMoviePlayerController的基本知识

    MPMoviePlayerController基本应用

  • 谷粒商城-微博登录

    微博应用基本信息 微博应用基本信息有几处是必填项,图片上传也有相应的上传要求。 基本信息 说明值应用名称自定义即可...

  • Linux基本应用

    虚拟终端切换 centos6 : ctrl + alt + F2~F6 /dev/tty2-6 centos7: ...

  • 注解基本应用

    注解: 注解目前非常的流行,很多主流框架都支持注解,而且自己编写代码的时候也会尽量的去用注解,既方便,也让代码更加...

  • GitLab基本应用

    1.安装GitLab 2.ssh-keygen生成秘钥文件.ssh/id_rsa.pub 3.GitLab常用命令...

  • 基本统计量应用

    案例 使用基本描述统计量计算存款金额的基本描述统计量,并分别对城镇储户和农村储户进行比较。 数据 实验体会 首先拿...

网友评论

    本文标题:Swift-Runtime 基本应用

    本文链接:https://www.haomeiwen.com/subject/sxbcettx.html