91精产品自偷自偷综合官网版下载-91精产品自偷自偷综合下-91精品-91精品91久久久-91精品成人-91精品成人www

網站建設資訊

NEWS

網站建設資訊

RxSwift中怎么替換delegate-創新互聯

這期內容當中小編將會給大家帶來有關RxSwift中怎么替換delegate,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創新互聯公司是一家朝氣蓬勃的網站建設公司。公司專注于為企業提供信息化建設解決方案。從事網站開發,網站制作,網站設計,網站模板,微信公眾號開發,軟件開發,微信平臺小程序開發,十余年建站對崗亭等多個行業,擁有豐富的網站建設經驗。

目標

最近寫項目 ,寫到需要為自己寫的一個控件添加rx訂閱方式的案例。

目前有一個代理:

// 代理方式獲取結果@objc public protocol ZZPhotoPickerControllerDelegate : NSObjectProtocol { @objc optional func photoPickerController(_ photoPickerController: ZZPhotoPickerController, didSelect assets: [Any])}

需要寫一個能夠實現下邊這種方式的擴展

photoPickerController.rx.assetsSelected.subscribe(onNext: { assets in // do something}

思路

剛開始完全摸不著頭腦。后來想到Rx寫了對UICollectionViewDelegate的擴展:

collectionView.rx.itemSelected.subscribe(onNext: { indexPath in // do something}

跟我的需求是一樣的。

于是就去看itemSelected的源代碼:

/// Reactive wrapper for `delegate` message `collectionView(_:didSelectItemAtIndexPath:)`. public var itemSelected: ControlEvent {  let source = delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didSelectItemAt:)))   .map { a in    return try castOrThrow(IndexPath.self, a[1])   }    return ControlEvent(events: source) }

souce是一個Observable,由delegate.methodInvoked產生。delegate是什么delegate?為什么會有methodInvoked方法?于是繼續點進去。

extension Reactive where Base: UIScrollView {   /// ...這部分代碼省略不用看  /// Reactive wrapper for `delegate`.  ///  /// For more information take a look at `DelegateProxyType` protocol documentation.  public var delegate: DelegateProxy {   return RxScrollViewDelegateProxy.proxy(for: base)  }    /// ...后面的代碼暫時也不用看}

可以看到delegate是一個DelegateProxy類型,根據字面是理解就是代理的代理。然后還看到這里的rx是擴展自UIScrollView的,UICollectionView是繼承自UIScrollView,可以知道這里的delegate也是繼承過來的使用的。還可以看到RxScrollViewDelegateProxy這個東西,可以想到如果我們要仿寫的話,自己也應該寫這樣一個代理的代理類。先點進去看看:

open class RxScrollViewDelegateProxy : DelegateProxy , DelegateProxyType  , UIScrollViewDelegate { /// Typed parent object. public weak private(set) var scrollView: UIScrollView? /// - parameter scrollView: Parent object for delegate proxy. public init(scrollView: ParentObject) {  self.scrollView = scrollView  super.init(parentObject: scrollView, delegateProxy: RxScrollViewDelegateProxy.self) } // Register known implementations public static func registerKnownImplementations() {  self.register { RxScrollViewDelegateProxy(scrollView: $0) }  self.register { RxTableViewDelegateProxy(tableView: $0) }  self.register { RxCollectionViewDelegateProxy(collectionView: $0) }  self.register { RxTextViewDelegateProxy(textView: $0) } } /// ...后面的感覺沒什么關系,先不看}

可以看到它其實是一個DelegateProxy,并且遵守了DelegateProxyType和UIScrollViewDelegate協議,可以感覺出它是一個鏈接rx和delegate的紐帶。有一個實例變量scrollView,有一個init方法,有一個registerKnownImplementations靜態方法。

現在腦海中大概有一個模糊的思路:我們要先創建一個紐帶delegateProxy對象,然后在目標類的rx擴展中創建一個delegateProxy實例,最后在我們的assetsSelected事件流中用這個delegateProxy的methodInvoked截獲delegate中的目標方法,并生成可訂閱的Observable返回給controlEvent,這樣鏈接打通。

開動

首先創建一個RxPhotoPickerControllerDelegateProxy

class RxPhotoPickerControllerDelegateProxy: DelegateProxy, DelegateProxyType, ZZPhotoPickerControllerDelegate {  /// Typed parent object. public weak private(set) var photoPickerController: ZZPhotoPickerController?  /// - parameter scrollView: Parent object for delegate proxy. public init(photoPickerController: ParentObject) {  self.photoPickerController = photoPickerController  super.init(parentObject: photoPickerController, delegateProxy: RxPhotoPickerControllerDelegateProxy.self) }  static func registerKnownImplementations() {  self.register { RxPhotoPickerControllerDelegateProxy(photoPickerController: $0) } }  // 把上面的寫好后,編輯器會提示你需要實現一下兩個方法,一個是獲取,一個是設置,所以很好理解該在方法里實現什么。 static func currentDelegate(for object: ZZPhotoPickerController) -> ZZPhotoPickerControllerDelegate? {  return object.zzDelegate }  static func setCurrentDelegate(_ delegate: ZZPhotoPickerControllerDelegate?, to object: ZZPhotoPickerController) {  object.zzDelegate = delegate } }

然后給目標的rx擴展寫一個delegateProxy實例:

extension Reactive where Base: ZZPhotoPickerController {  public var zzDelegate: DelegateProxy {  return RxPhotoPickerControllerDelegateProxy.proxy(for: base) } }

最后寫我們的assetsSelected:

extension Reactive where Base: ZZPhotoPickerController {  var assetsSelected: ControlEvent<[Any]> {  let source: Observable<[Any]> = self.zzDelegate.methodInvoked(#selector(ZZPhotoPickerControllerDelegate.photoPickerController(_:didSelect:))).map { a in   return a[1] as! [Any]  }  return ControlEvent.init(events: source) } }

要注意里面有個方法castOrThrow,這個方法rx并沒有開放出來,是個內部方法,如果照著寫報錯。可以研究出該方法只是一個類型推斷而已,所以可以簡單寫。

上述就是小編為大家分享的RxSwift中怎么替換delegate了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。


網頁題目:RxSwift中怎么替換delegate-創新互聯
URL地址:http://www.yuzhuanjia.cn/article/ejpih.html
主站蜘蛛池模板: 91欧美激情一区二区三 | 91久久人澡 | 国产v无码专区亚洲v手机麻豆 | av片无码一区二区不卡电影 | 91久久午夜无码 | 99久久久久久97 | 91精品国产综合久久久亚州日韩 | 91精品综合在线偷观看视频 | 国产爆乳无码一区二区三区 | 国产91专区一 | 91精品国产午夜福利在线观看 | 午夜福利电影av | 91精品国产欧美一区二区 | 99欧美日韩精品综合色 | 丰满人妻妇伦 | a级全黄又爽又黄的网站 | av资源站最稳定的资源站 | 精品久久久久久久 | 99热免费观看| 91日日日日| 午夜在线视频91精品 | av麻豆免费在线观 | 午夜高清无码在线 | 91免费永久 | 波多野结衣多次高潮三个老人 | 国产av一区二区精品 | 91精品青草福利久久 | av天堂电影网| 91无码人妻一区二区成人aⅴ | 99久久精品免费观看国产一区 | 高清国产在线拍揄自揄视频 | 99久久精品影院老鸭窝 | 国产av一二三四又爽又色又色 | 一区二区三区国产区小说 | 国产av影片网址 | 99久久国产精品免费人妻 | 午夜视频一区 | 99免费视频观看 | av中文字幕在线观看 | 午夜高清国产拍精品 | 91精品国产手机在 |