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 @   M·emor·Y  阅读(62)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示