/StronglyTypedNotifications

Strongly typed notifications for ObjC with lightweight generics features

Primary LanguageObjective-CMIT LicenseMIT

STNotifications: Strongly Typed Notifications for Objective C

Build status Code coverage status CocoaPods Carthage

STNotifications is Objective-C library for Strongly Typed Notifications

Features

  • Strongly typed payload (using safe api around Objective-C lightweight generics)
  • Makes sure only one NSNotificationName exists for Notification and Observer
  • You have pointer to sender from recieved notification!
  • Interoperable with Swift!
  • Autounsubscribtion on NotificationToken deallocation!

Usage

Create factory method

#import <STNotifications/STNotifications.h>
@class Alert;

@interface STNotificationFactory (YourCustomNotifications)
+ (STNotificationFactory<Payload *> *)payloadFactory;
@end

@implementation STNotificationFactory (YourCustomNotifications)
+ (STNotificationFactory<Payload *> *)payloadFactory {
    return [STNotificationFactory factoryWithNotificationName:@"Notification payload"];
}
@end

or your own subclass:

#import <STNotifications/STNotifications.h>
#import "Alert.h"

@interface AlertNotificationFactory : STNotificationFactory <Alert *>
- (instancetype)init;
+ (instancetype)factory;
@end

@implementation AlertNotificationFactory

- (instancetype)init {
    return self = [super initWithNotificationName:@"AlertNotification"];
}
+ (instancetype)factory {
    return [[AlertNotificationFactory alloc] init];
}
@end

That's it! Now your NotificationName and Payload Type can be no longer be messed up!

Start observing

Autocompletion will insert payload type you specified in factory same way as it's in NSArray!  STNotifications: Strongly Typed Notifications for Objective C

  @property (strong, nonatomic) STNotificationToken *token; // < ---- Auto Unsubscription on deallocation!
  ...
  let factory = [AlertNotificationFactory new];
  let alertObserver = [factory makeObserverWithOnRecievedBlock:^(STNotification<Alert *> * _Nullable notification) {
    NSLog(@"%@", notification.payload.message);
    NSLog(@"%@", notification.sender);
  }];
  self.alertToken = [[NSNotificationCenter defaultCenter] stn_addNotificationObserver:alertObserver];

You can track down sender of notification! STNotificationToken has autounsubscription feature on deallocation!

Post notification

You can no longer mess up payload type!  STNotifications: Strongly Typed Notifications for Objective C

  var alert = [Alert new];
  alert.message = @"ALARM!!!";

  let alertFactory = [AlertNotificationFactory new];
  let alertNotification = [alertFactory makeNotificationWithPayload:alert sender:self];
  [[NSNotificationCenter defaultCenter] stn_postNotification:alertNotification];

Installation

CocoaPods

target '<Your Target Name>' do
    pod 'STNotifications', '~> 1.1.5.1'
end

Carthage

github "neisip/StronglyTypedNotifications"

License

STNotifications is released under the MIT license. See LICENSE for details.