Swift中Notification.Name自定义枚举

/// 自定义通知枚举类型
enum JYNotification: String {
    ///刷新员工列表
    case refreshStaffList
    
    var stringValue: String {
        return "JY" + rawValue
    }
    
    /// 通知名称
    var notificationName: NSNotification.Name {
        return NSNotification.Name(stringValue)
    }
}

extension NotificationCenter {
    
    /// 自定义通知方法
    /// - Parameters:
    ///   - nameType:通知类型
    ///   - object: 对象
    static func post(customeNotificationType nameType: JYNotification, object: Any? = nil){
        NotificationCenter.default.post(name: nameType.notificationName, object: object)
    }
}

 NotificationCenter.post(customeNotificationType: JYNotification.refreshStaffList)

参考:

Swift中Notification.Name这么难用怎么办

https://www.jianshu.com/p/105f6b133bd2

原文地址:https://www.cnblogs.com/qingzZ/p/12758922.html