Latest 1.1.2
Homepage https://github.com/neisip/StronglyTypedNotifications
License MIT
Platforms ios 8.0
Authors

STNotifications: Strongly Typed Notifications for Objective C

Build status
Code coverage status
CocoaPods
Carthage

STNotifications is Objective-C library for Strongly Typed Notifications

Features

  • [x] Strongly typed payload (using safe api around Objective-C lightweight generics)
  • [x] Makes sure only one NSNotificationName exists for Notification and Observer
  • [x] You have pointer to sender from recieved notification!
  • [x] Interoperable with Swift!
  • [x] 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 "Payload.h"

@interface PayloadNotificationFactory : STNotificationFactory <Payload *>
+ (instancetype)factory;
@end

#import "PayloadNotificationFactory.h"

@implementation PayloadNotificationFactory

+ (instancetype)factory {
    return [PayloadNotificationFactory factoryWithNotificationName:@"PayloadNotification"];
}
@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!

 STNotificationObserver *observer = [[AlertNotificationFactory factory] makeObserverWithOnRecievedBlock:^(STNotification<Alert *> * _Nullable notification) {
        NSLog(@"%@", notification.payload.message);
        NSLog(@"%@", notification.sender);
    }];
self.token = [[NSNotificationCenter defaultCenter] stn_addNotificationObserver:observer];

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

  Alert *alert = [Alert new];
  alert.message = @"ALARM!!!";
  STNotification *alertNotification = [[AlertNotificationFactory factory] makeNotificationWithPayload:alert sender:self];
  [[NSNotificationCenter defaultCenter] stn_postNotification:alertNotification];

Installation

CocoaPods

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

Carthage

github "neisip/StronglyTypedNotifications"

License

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

Latest podspec

{
    "name": "STNotifications",
    "version": "1.1.2",
    "summary": "Tiny library for Strongly Typed Notifications in Objective-C",
    "description": "Strongly typed notificatinos allow your Objective-C notifications to work in a safe manner, using light generics magics and little bit of architecture tricks!nBenefits:n1) Strongly typed payloadn2) NSNotification name won't be messed again!n3) Easy syntax for usen4) Interoperable with swift!n5) 100% Code Coverage!",
    "homepage": "https://github.com/neisip/StronglyTypedNotifications",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alexander Kazartsev": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/neisip/StronglyTypedNotifications.git",
        "tag": "1.1.2"
    },
    "source_files": [
        "Classes",
        "STNotifications/**/*.{h,m}"
    ],
    "exclude_files": "Classes/Exclude"
}

Pin It on Pinterest

Share This