欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

swift中利用runtime交換方法的實(shí)現(xiàn)示例

 更新時(shí)間:2018年05月08日 11:07:01   作者:小峰書(shū)  
這篇文章主要給大家介紹了關(guān)于swift中利用runtime交換方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

Runtime介紹

學(xué)習(xí)一個(gè)東西至少要先知道它是個(gè)啥,你一定聽(tīng)說(shuō)過(guò)“運(yùn)行時(shí)是 Objective-C 的一個(gè)特色”,這里的“運(yùn)行時(shí)”就是指 runtime 了。

老的方式initialize現(xiàn)在已經(jīng)不適用了,需要用新的方式代替。

思路: 定義一個(gè)啟動(dòng)的協(xié)議,在app完成啟動(dòng)的方法里把需要做method swizzle的類(lèi)跑一邊協(xié)議的方法

第一種

1、Step One

 protocol SelfAware: class {
  static func awake()
 }

 class NothingToSeeHere {
  static func harmlessFunction() {
   let typeCount = Int(objc_getClassList(nil, 0))
   let types = UnsafeMutablePointer<AnyClass?>.allocate(capacity: typeCount)
   let autoreleasingTypes = AutoreleasingUnsafeMutablePointer<AnyClass?>(types)
    objc_getClassList(autoreleasingTypes, Int32(typeCount))
   for index in 0 ..< typeCount { (types[index] as? SelfAware.Type)?.awake() }
   types.deallocate(capacity: typeCount)
  }
 }

2、step two

 extension UIApplication {
   private static let runOnce: Void = {
    NothingToSeeHere.harmlessFunction()
   }()

  override open var next: UIResponder? {
    // Called before applicationDidFinishLaunching
    UIApplication.runOnce
    return super.next
  }
 } 

3、step three

遵循協(xié)議SelfAware,實(shí)現(xiàn)awake()

第二種(類(lèi)似第一種)

1、創(chuàng)建一個(gè)swizzle注入的協(xié)議

public protocol SwizzlingInjection: class {
 static func inject()
}

2、創(chuàng)建swizzle helper

open class SwizzlingManager {
 //只會(huì)調(diào)用一次的方法
 private static let doOnce: Any? = {
  UIViewController.inject()
  return nil
 }()
 
 open static func enableInjection() {
  _ = SwizzlingManager.doOnce
 }
}

3、給UIApplication 創(chuàng)建分類(lèi)調(diào)用那個(gè)一次方法

extension UIApplication{
 open override var next: UIResponder?{
  SwizzlingManager.enableInjection()
  return super.next
 }
}

4、在你需要的類(lèi)中遵循注入?yún)f(xié)議

extension UIViewController: SwizzlingInjection{
  public static func inject() {
  //確保不是子類(lèi)
  guard self === UIViewController.self else { return }
  
  DispatchQueue.once(token: "com.moglo.urmoji.UIViewController") {
   //do swizzle method
  }
 }
}

once只執(zhí)行一次的方法

public extension DispatchQueue { 
 private static var _onceTracker = [String]() 
 public class func once(file: String = #file, function: String = #function, line: Int = #line, block:()->Void) {
  let token = file + ":" + function + ":" + String(line)
  once(token: token, block: block)
 }
 
 /**
  Executes a block of code, associated with a unique token, only once. The code is thread safe and will
  only execute the code once even in the presence of multithreaded calls.  
  - parameter token: A unique reverse DNS style name such as com.vectorform.<name> or a GUID
  - parameter block: Block to execute once
  */
 public class func once(token: String, block:()->Void) {
  objc_sync_enter(self)
  defer { objc_sync_exit(self) }
  if _onceTracker.contains(token) {
   return
  }
  _onceTracker.append(token)
  block()
 }

 //delay
 typealias Task = (_ cancel : Bool) -> Void
 @discardableResult
 static func delay(time : TimeInterval, task: @escaping () -> ()) -> Task? {
  
  func dispatch_later(block : @escaping () -> ()) {
   DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time , execute: block)
  }
  
  var closure : (() -> ())? = task
  var result : Task?
  let delayedClosure : Task = {
   cancel in
   if let internalClosure = closure {
    if cancel == false {
     DispatchQueue.main.async(execute: internalClosure)
    }
   }
   closure = nil
   result = nil
  }
  
  result = delayedClosure
  dispatch_later { () -> () in
   if let delayedClosure = result {
    delayedClosure(false)
   }
  }
  return result
 }
 
 static func cancel(task : Task?) {
  task?(true)
 }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 使用?Swift?Package?插件生成代碼的示例詳解

    使用?Swift?Package?插件生成代碼的示例詳解

    這篇文章主要介紹了使用?Swift?Package?插件生成代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Swift心得筆記之運(yùn)算符

    Swift心得筆記之運(yùn)算符

    區(qū)別于 C 語(yǔ)言,在 Swift 中你可以對(duì)浮點(diǎn)數(shù)進(jìn)行取余運(yùn)算(%),Swift 還提供了 C 語(yǔ)言沒(méi)有的表達(dá)兩數(shù)之間的值的區(qū)間運(yùn)算符,(a..b和a...b),這方便我們表達(dá)一個(gè)區(qū)間內(nèi)的數(shù)值。
    2015-04-04
  • Swift教程之屬性詳解

    Swift教程之屬性詳解

    這篇文章主要介紹了Swift教程之屬性詳解,屬性是描述特定類(lèi)、結(jié)構(gòu)或者枚舉的值,計(jì)算屬性存在于類(lèi)、結(jié)構(gòu)與枚舉中,存儲(chǔ)屬性?xún)H僅只在類(lèi)與結(jié)構(gòu)中,需要的朋友可以參考下
    2015-01-01
  • Swift編程中數(shù)組的使用方法指南

    Swift編程中數(shù)組的使用方法指南

    這篇文章主要介紹了Swift編程中數(shù)組的使用方法指南,是Swift入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-11-11
  • 深入講解Swift中的模式匹配

    深入講解Swift中的模式匹配

    在Swift中,一些模式已經(jīng)被語(yǔ)言特性所吸收,你在使用Swift甚至察覺(jué)不出這類(lèi)問(wèn)題的存在,下面這篇文章主要給大家介紹了關(guān)于Swift中模式匹配的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-08-08
  • 初步理解Swift中的泛型

    初步理解Swift中的泛型

    這篇文章主要介紹了初步理解Swift中的泛型,是Swift入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-07-07
  • Swift類(lèi)型創(chuàng)建之自定義一個(gè)類(lèi)型詳解

    Swift類(lèi)型創(chuàng)建之自定義一個(gè)類(lèi)型詳解

    這篇文章主要介紹了Swift類(lèi)型創(chuàng)建之自定義一個(gè)類(lèi)型詳解,本文講解了自定義原型、實(shí)現(xiàn)默認(rèn)值、支持基本布爾型初始化、支持Bool類(lèi)型判斷、支持兼容各們各派的類(lèi)型、完善OCBool的布爾基因體系等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能

    Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能

    這篇文章主要介紹了Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • Swift自定義iOS中的TabBarController并為其添加動(dòng)畫(huà)

    Swift自定義iOS中的TabBarController并為其添加動(dòng)畫(huà)

    這篇文章主要介紹了Swift自定義iOS中的TabBarController并為其添加動(dòng)畫(huà)的方法,即自定義TabBarController中的的TabBar并為自定義的TabBar增加動(dòng)畫(huà)效果,需要的朋友可以參考下
    2016-04-04
  • Swift實(shí)現(xiàn)代碼混淆詳解

    Swift實(shí)現(xiàn)代碼混淆詳解

    本文詳細(xì)講解了Swift實(shí)現(xiàn)代碼混淆的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2021-11-11

最新評(píng)論