Swift开发-NSNotification.Name 写法

54 阅读1分钟

NSNotification.Name 结合extension和struct 根据不同业务模块细化通知名,一目了然, 项目中这样管理通知名。


import Foundation


extension Notification.Name {

    struct User {

        static let CancelLogin = Notification.Name(rawValue: "org.taozel.notification.name.user.cancelLogin")

    }

    struct Community {

        static let RefreshPostList = Notification.Name(rawValue: "org.taozel.notification.name.community.refreshPostList")

    }
    
    struct IM {

        static let login = Notification.Name(rawValue: "org.taozel.notification.im.login")

    }

}

这样调用


 NotificationCenter.default.post(name: NSNotification.Name.Community.RefreshPostList, object: nil)