in actual development, NSNotification can well decouple code and transmit data across layers. But because it is globally effective. Therefore, sometimes we don't want to have such a wide range of functions, but just want to send and receive notifications within a certain range. Recently, I made a plan to share with you. My side is mainly divided into two parts
Takes effect within the scope of a class
in the actual development process, sometimes the scope of notification only takes effect within the scope of a class, that is, it will take effect within the object created by this class. During the running of app, there may be multiple objects of a class at the same time. In this case, I associate the sending and receiving of notifications with the class name. This ensures that the notice will only take effect to the extent of such relevant. The specific codes are as follows:
OC:
///Send notifications within a certain range ///@ param moduleName module name ///@ param notificationName the name of the notification - (void)jk_postNotificationAtModule:(NSString *)moduleName notificationName:(NSString *)notificationName; ///Send notifications within a certain range ///@ param moduleName module name ///@ param notificationName the name of the notification /// @param object object /// @param userInfo userInfo - (void)jk_postNotificationAtModule:(NSString *)moduleName notificationName:(NSString *)notificationName object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo; ///Listen for notification events within a certain range ///@ param moduleName module name ///@ param name notification name ///@ param block notification event handling - (void)jk_observeNotificationAtModule:(NSString *)moduleName forName:(NSString *)name usingBlock:(void(^)(NSNotification *notification))block; ///Listen for a set of notification events within a certain range ///@ param moduleName module name ///@ param names notification name array ///@ param block notification event handling - (void)jk_observeNotificationAtModule:(NSString *)moduleName forNames:(NSArray<NSString *> *)names usingBlock:(void(^)(NSNotification *notification))block;
Note: moduleName is a string that has a one-to-one correspondence with a class, or it can be a user-defined module name. It should be consistent when sending and receiving notifications
swift:
///Send notifications within a certain range /// - Parameters: ///- moduleName: module name ///- notificationName: notification name func jk_postNotification(at moduleName:String, notificationName:String) -> Void ///Send notifications within a certain range /// - Parameters: ///- moduleName: module name ///- notificationName: notification name /// - object: object /// - userInfo: userInfo func jk_postNotification(at moduleName:String, notificationName:String, object:Any?, userInfo:[AnyHashable : Any]?) -> Void ///Listen for notification events within a certain range ///@ param moduleName module name ///@ param name notification name ///@ param block notification event handling - (void)jk_observeNotificationAtModule:(NSString *)moduleName forName:(NSString *)name usingBlock:(void(^)(NSNotification *notification))block; ///Listen for a set of notification events within a certain range ///@ param moduleName module name ///@ param names notification name array ///@ param block notification event handling - (void)jk_observeNotificationAtModule:(NSString *)moduleName forNames:(NSArray<NSString *> *)names usingBlock:(void(^)(NSNotification *notification))block;
Note: moduleName is a string that has a one-to-one correspondence with a class, or it can be a user-defined module name. It should be consistent when sending and receiving notifications
Takes effect within the scope of an object
in the actual development process, sometimes the scope of action that only wants to be notified is only valid within the instance object scope of a class, and even other instance objects of the same class are not valid. The specific codes are as follows:
OC:
///Send notifications within a certain range ///@ param moduleInstance module instance object ///@ param notificationName the name of the notification - (void)jk_postNotificationAtModuleInstance:(__kindof NSObject *)moduleInstance notificationName:(NSString *)notificationName; ///Send notifications within a certain range ///@ param moduleInstance module instance object ///@ param notificationName the name of the notification /// @param object object /// @param userInfo userInfo - (void)jk_postNotificationAtModuleInstance:(__kindof NSObject *)moduleInstance notificationName:(NSString *)notificationName object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo; ///Listen for notification events within a certain range ///@ param moduleInstance module instance object ///@ param name notification name ///@ param block notification event handling - (void)jk_observeNotificationAtModuleInstance:(__kindof NSObject *)moduleInstance forName:(NSString *)name usingBlock:(void(^)(NSNotification *notification))block; ///Listen for a set of notification events within a certain range ///@ param moduleInstance module instance object ///@ param names notification name array ///@ param block notification event handling - (void)jk_observeNotificationAtModuleInstance:(__kindof NSObject *)moduleInstance forNames:(NSArray<NSString *> *)names usingBlock:(void(^)(NSNotification *notification))block;
Note: moduleInstance is an instance object of a class. Keep the consistency between sending and receiving notifications
swift:
///Send notifications within a certain range /// - Parameters: ///- moduleInstance: module instance object ///- notificationName: notification name func jk_postNotification(at moduleInstance:Any, notificationName:String) -> Void ///Send notifications within a certain range /// - Parameters: ///- moduleInstance: module instance object ///- notificationName: notification name /// - object: object /// - userInfo: userInfo func jk_postNotification(at moduleInstance:Any, notificationName:String, object:Any?, userInfo:[AnyHashable : Any]?) -> Void ///Listen for notification events within a certain range /// - Parameters: ///- moduleInstance: module instance object ///- notificationName: notification name ///- block: notification event handling func jk_observeNotification(at moduleInstance:Any, notificationName:String, block:@escaping ((_ notification:Notification) -> Void)) ->Void ///Listen for notification events within a certain range /// - Parameters: ///- moduleInstance: module instance object ///- notificationNames: array of notification names ///- block: notification event callback func jk_observeNotifications(at moduleInstance:Any, notificationNames:Array<String>, block:@escaping ((_ notification:Notification) -> Void)) ->Void
Note: moduleInstance is an instance object of a class. Keep the consistency between sending and receiving notifications
pod integration commands are as follows:
OC:
pod 'JKNoticationHelper'
swift:
pod 'JKNoticationHelper_Swift'
Source code download address: https://github.com/xindizhiyin2014/JKNoticationHelper.git