swift - 命名空间拓展

/// 命名空间
public final class DCNameSpace<Base> {
    
    internal var base: Base
    
    internal var BASE: DCNameSpace<Base>.Type
    
    init(_ base: Base) {
        
        self.base = base
        
        self.BASE = DCNameSpace<Base>.self
        
    }
    
}


/// 命名空间协议
public protocol DCNameSpaceProtocol {
    
    associatedtype TargetType
    
    
    /// 实例变量及方法命名空间
    var dc: DCNameSpace<TargetType> { get }
    
    
    /// 类变量及方法命名空间
    static var dc: DCNameSpace<TargetType>.Type { get }
    
}

 

//
//  DCCompatible.swift
//  DCAlertSomeUIKit
//
//  Created by baitongtong on 2022/6/7.
//

import UIKit


extension DCNameSpaceProtocol {
  
    
    public var dc: DCNameSpace<Self> {
        
        get {
            
            DCNameSpace<Self>(self)
            
        }
        
    }
    
    public static var dc: DCNameSpace<Self>.Type {
        
        get {
            
            DCNameSpace<Self>.self
            
        }
        
    }
    
}

// 要扩展的类
extension NSObject : DCNameSpaceProtocol {}
extension CGFloat : DCNameSpaceProtocol {}

 

//
//  DCUIApplication+Extension.swift
//  DCAlertSomeUIKit
//
//  Created by guaker on 2022/6/9.
//

import UIKit

extension DCNameSpace where Base == UIApplication {
    
    public func topViewController(_ controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        
        if let navigationController = controller as? UINavigationController {
            
            if let visible = navigationController.visibleViewController {
                return topViewController(visible)
            }
        }
        
        if let tabControler = controller as? UITabBarController {
            if let selected = tabControler.selectedViewController {
                return topViewController(selected)
            }
        }
        
        if let presented = controller?.presentedViewController {
            return topViewController(presented)
        }
        
        return controller
    }
}

 

posted @ 2022-06-13 17:27  M·emor·Y  阅读(60)  评论(0编辑  收藏  举报