├── .gitignore ├── Example ├── App │ ├── AppDelegate.swift │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift └── TypedNotifications.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── TypedNotifications │ └── TypedNotification.swift ├── Tests └── TypedNotificationsTests │ └── TypedNotificationTests.swift └── TypedNotifications.podspec /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /Example/App/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TypedNotifications 4 | // 5 | // Created by Joe Fabisevich on 8/31/17. 6 | // Copyright © 2017 Mergesort. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Example/App/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Example/App/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Example/App/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 30 | 31 | 32 | 33 | 43 | 53 | 62 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /Example/App/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Example/App/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TypedNotifications 4 | // 5 | // Created by Joe Fabisevich on 8/30/17. 6 | // Copyright © 2017 Mergesort. All rights reserved. 7 | // 8 | 9 | import os.log 10 | import UIKit 11 | import TypedNotifications 12 | 13 | // There are only 3 jobs in the world, trust me. 14 | enum Job { 15 | 16 | case softwareDeveloper 17 | case designer 18 | case conArtist 19 | 20 | var title: String { 21 | switch self { 22 | 23 | case .softwareDeveloper: 24 | return "Software Developer" 25 | 26 | case .designer: 27 | return "Designer" 28 | 29 | case .conArtist: 30 | return "Con Artist" 31 | 32 | } 33 | } 34 | 35 | } 36 | 37 | struct Person { 38 | 39 | let name: String 40 | let job: Job 41 | 42 | } 43 | 44 | /// A `TypedNotification` with no payload. 45 | struct PayloadFreeTypedNotification: TypedNotification {} 46 | 47 | /// A very simple payload to send. 48 | /// Just contains one boolean value. 49 | struct TypedBooleanNotification: TypedPayloadNotification { 50 | 51 | let payload: Bool 52 | 53 | } 54 | 55 | /// A slightly more complicated payload to send. 56 | /// It contains a struct as it's payload. 57 | struct TypedPersonNotification: TypedPayloadNotification { 58 | 59 | let payload: Person 60 | 61 | } 62 | 63 | /// A more complicated payload to send. 64 | /// It contains any generic value you want to send, and recieve. 65 | struct TypedGenericNotification: TypedPayloadNotification { 66 | 67 | let payload: T 68 | 69 | } 70 | 71 | class ViewController: UIViewController { 72 | 73 | @IBOutlet var currentNotificationLabel: UILabel! 74 | 75 | override func viewDidLoad() { 76 | super.viewDidLoad() 77 | 78 | NotificationCenter.default.register(type: TypedBooleanNotification.self, observer: self, selector: #selector(booleanWasReceived)) 79 | NotificationCenter.default.register(type: TypedPersonNotification.self, observer: self, selector: #selector(personWasReceived)) 80 | NotificationCenter.default.register(type: TypedGenericNotification.self, observer: self, selector: #selector(stringWasReceived)) 81 | 82 | NotificationCenter.default.register(type: PayloadFreeTypedNotification.self, observer: self, selector: #selector(payloadFreeWasReceived)) 83 | } 84 | 85 | } 86 | 87 | private extension ViewController { 88 | 89 | @IBAction func tappedSendPeopleButton() { 90 | let amanda = Person(name: "Amanda", job: .softwareDeveloper) 91 | let amandaNotification = TypedPersonNotification(payload: amanda) 92 | 93 | NotificationCenter.default.post(typedNotification: amandaNotification) 94 | 95 | let erica = Person(name: "Erica", job: .designer) 96 | let ericaNotification = TypedPersonNotification(payload: erica) 97 | 98 | DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) { 99 | NotificationCenter.default.post(typedNotification: ericaNotification) 100 | } 101 | 102 | let joe = Person(name: "Joe", job: .conArtist) 103 | let joeNotification = TypedPersonNotification(payload: joe) 104 | 105 | DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4)) { 106 | NotificationCenter.default.post(typedNotification: joeNotification) 107 | } 108 | } 109 | 110 | @IBAction func tappedSendPayloadFreeButton() { 111 | let payloadFreeNotification = PayloadFreeTypedNotification() 112 | NotificationCenter.default.post(typedNotification: payloadFreeNotification) 113 | } 114 | 115 | @IBAction func tappedSendBooleanButton() { 116 | let booleanNotification = TypedBooleanNotification(payload: true) 117 | NotificationCenter.default.post(typedNotification: booleanNotification) 118 | } 119 | 120 | @IBAction func tappedGenericValuesButton() { 121 | let genericNotification = TypedGenericNotification(payload: "This is a payload") 122 | NotificationCenter.default.post(typedNotification: genericNotification) 123 | } 124 | 125 | @objc func payloadFreeWasReceived(notification: Notification) { 126 | self.currentNotificationLabel.text = "Got our payload free notification!" 127 | } 128 | 129 | @objc func booleanWasReceived(notification: Notification) { 130 | guard let boolean = notification.getPayload(notificationType: TypedBooleanNotification.self) else { 131 | os_log("Could not properly retrieve payload from BooleanTypedNotification") 132 | return 133 | } 134 | 135 | self.currentNotificationLabel.text = "Got our Bool payload!\n\(boolean)" 136 | } 137 | 138 | @objc func personWasReceived(notification: Notification) { 139 | guard let person = notification.getPayload(notificationType: TypedPersonNotification.self) else { 140 | os_log("Could not properly retrieve payload from PersonTypedNotification") 141 | return 142 | } 143 | 144 | let nameText = "Name: \(person.name)" 145 | let jobText = "Job: \(person.job.title)" 146 | 147 | self.currentNotificationLabel.text = "Got our Person payload!\n\(nameText)\n\(jobText)" 148 | } 149 | 150 | @objc func stringWasReceived(notification: Notification) { 151 | guard let string = notification.getPayload(notificationType: TypedGenericNotification.self) else { 152 | os_log("Could not properly retrieve payload from GenericTypedNotification") 153 | return 154 | } 155 | 156 | self.currentNotificationLabel.text = "Got our generic payload!\n\(string)" 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /Example/TypedNotifications.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 52; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 66C64D4922CA3AE20013B62E /* TypedNotifications in Frameworks */ = {isa = PBXBuildFile; productRef = 66C64D4822CA3AE20013B62E /* TypedNotifications */; }; 11 | 66C64D4E22CA3BB10013B62E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B7EBAD151F5871A600A08C26 /* LaunchScreen.storyboard */; }; 12 | 66C64D4F22CA3BB30013B62E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B7EBAD101F5871A600A08C26 /* Main.storyboard */; }; 13 | B7EBAD0D1F5871A600A08C26 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7EBAD0C1F5871A600A08C26 /* AppDelegate.swift */; }; 14 | B7EBAD0F1F5871A600A08C26 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7EBAD0E1F5871A600A08C26 /* ViewController.swift */; }; 15 | B7EBAD141F5871A600A08C26 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B7EBAD131F5871A600A08C26 /* Assets.xcassets */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 66C64D4522CA3ABA0013B62E /* TypedNotifications */ = {isa = PBXFileReference; lastKnownFileType = folder; name = TypedNotifications; path = ..; sourceTree = ""; }; 20 | B7EBAD091F5871A600A08C26 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | B7EBAD0C1F5871A600A08C26 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | B7EBAD0E1F5871A600A08C26 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 23 | B7EBAD111F5871A600A08C26 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 24 | B7EBAD131F5871A600A08C26 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | B7EBAD161F5871A600A08C26 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | B7EBAD181F5871A600A08C26 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | B7EBAD061F5871A600A08C26 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | 66C64D4922CA3AE20013B62E /* TypedNotifications in Frameworks */, 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 66C64D4722CA3AE20013B62E /* Frameworks */ = { 42 | isa = PBXGroup; 43 | children = ( 44 | ); 45 | name = Frameworks; 46 | sourceTree = ""; 47 | }; 48 | B7EBAD001F5871A500A08C26 = { 49 | isa = PBXGroup; 50 | children = ( 51 | 66C64D4522CA3ABA0013B62E /* TypedNotifications */, 52 | B7EBAD0B1F5871A600A08C26 /* App */, 53 | B7EBAD0A1F5871A600A08C26 /* Products */, 54 | 66C64D4722CA3AE20013B62E /* Frameworks */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | B7EBAD0A1F5871A600A08C26 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | B7EBAD091F5871A600A08C26 /* Example.app */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | B7EBAD0B1F5871A600A08C26 /* App */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | B7EBAD0C1F5871A600A08C26 /* AppDelegate.swift */, 70 | B7EBAD0E1F5871A600A08C26 /* ViewController.swift */, 71 | B7EBAD101F5871A600A08C26 /* Main.storyboard */, 72 | B7EBAD131F5871A600A08C26 /* Assets.xcassets */, 73 | B7EBAD151F5871A600A08C26 /* LaunchScreen.storyboard */, 74 | B7EBAD181F5871A600A08C26 /* Info.plist */, 75 | ); 76 | path = App; 77 | sourceTree = ""; 78 | }; 79 | /* End PBXGroup section */ 80 | 81 | /* Begin PBXNativeTarget section */ 82 | B7EBAD081F5871A600A08C26 /* Example */ = { 83 | isa = PBXNativeTarget; 84 | buildConfigurationList = B7EBAD261F5871A600A08C26 /* Build configuration list for PBXNativeTarget "Example" */; 85 | buildPhases = ( 86 | B7EBAD051F5871A600A08C26 /* Sources */, 87 | B7EBAD061F5871A600A08C26 /* Frameworks */, 88 | B7EBAD071F5871A600A08C26 /* Resources */, 89 | ); 90 | buildRules = ( 91 | ); 92 | dependencies = ( 93 | 66C64D4D22CA3B0C0013B62E /* PBXTargetDependency */, 94 | ); 95 | name = Example; 96 | packageProductDependencies = ( 97 | 66C64D4822CA3AE20013B62E /* TypedNotifications */, 98 | ); 99 | productName = TypedNotifications; 100 | productReference = B7EBAD091F5871A600A08C26 /* Example.app */; 101 | productType = "com.apple.product-type.application"; 102 | }; 103 | /* End PBXNativeTarget section */ 104 | 105 | /* Begin PBXProject section */ 106 | B7EBAD011F5871A500A08C26 /* Project object */ = { 107 | isa = PBXProject; 108 | attributes = { 109 | LastSwiftUpdateCheck = 0900; 110 | LastUpgradeCheck = 1010; 111 | ORGANIZATIONNAME = Mergesort; 112 | TargetAttributes = { 113 | B7EBAD081F5871A600A08C26 = { 114 | CreatedOnToolsVersion = 9.0; 115 | LastSwiftMigration = 1020; 116 | ProvisioningStyle = Automatic; 117 | }; 118 | }; 119 | }; 120 | buildConfigurationList = B7EBAD041F5871A500A08C26 /* Build configuration list for PBXProject "TypedNotifications" */; 121 | compatibilityVersion = "Xcode 8.0"; 122 | developmentRegion = en; 123 | hasScannedForEncodings = 0; 124 | knownRegions = ( 125 | en, 126 | Base, 127 | ); 128 | mainGroup = B7EBAD001F5871A500A08C26; 129 | productRefGroup = B7EBAD0A1F5871A600A08C26 /* Products */; 130 | projectDirPath = ""; 131 | projectRoot = ""; 132 | targets = ( 133 | B7EBAD081F5871A600A08C26 /* Example */, 134 | ); 135 | }; 136 | /* End PBXProject section */ 137 | 138 | /* Begin PBXResourcesBuildPhase section */ 139 | B7EBAD071F5871A600A08C26 /* Resources */ = { 140 | isa = PBXResourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | B7EBAD141F5871A600A08C26 /* Assets.xcassets in Resources */, 144 | 66C64D4E22CA3BB10013B62E /* LaunchScreen.storyboard in Resources */, 145 | 66C64D4F22CA3BB30013B62E /* Main.storyboard in Resources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXResourcesBuildPhase section */ 150 | 151 | /* Begin PBXSourcesBuildPhase section */ 152 | B7EBAD051F5871A600A08C26 /* Sources */ = { 153 | isa = PBXSourcesBuildPhase; 154 | buildActionMask = 2147483647; 155 | files = ( 156 | B7EBAD0F1F5871A600A08C26 /* ViewController.swift in Sources */, 157 | B7EBAD0D1F5871A600A08C26 /* AppDelegate.swift in Sources */, 158 | ); 159 | runOnlyForDeploymentPostprocessing = 0; 160 | }; 161 | /* End PBXSourcesBuildPhase section */ 162 | 163 | /* Begin PBXTargetDependency section */ 164 | 66C64D4D22CA3B0C0013B62E /* PBXTargetDependency */ = { 165 | isa = PBXTargetDependency; 166 | productRef = 66C64D4C22CA3B0C0013B62E /* TypedNotifications */; 167 | }; 168 | /* End PBXTargetDependency section */ 169 | 170 | /* Begin PBXVariantGroup section */ 171 | B7EBAD101F5871A600A08C26 /* Main.storyboard */ = { 172 | isa = PBXVariantGroup; 173 | children = ( 174 | B7EBAD111F5871A600A08C26 /* Base */, 175 | ); 176 | name = Main.storyboard; 177 | sourceTree = ""; 178 | }; 179 | B7EBAD151F5871A600A08C26 /* LaunchScreen.storyboard */ = { 180 | isa = PBXVariantGroup; 181 | children = ( 182 | B7EBAD161F5871A600A08C26 /* Base */, 183 | ); 184 | name = LaunchScreen.storyboard; 185 | sourceTree = ""; 186 | }; 187 | /* End PBXVariantGroup section */ 188 | 189 | /* Begin XCBuildConfiguration section */ 190 | B7EBAD241F5871A600A08C26 /* Debug */ = { 191 | isa = XCBuildConfiguration; 192 | buildSettings = { 193 | ALWAYS_SEARCH_USER_PATHS = NO; 194 | CLANG_ANALYZER_NONNULL = YES; 195 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 201 | CLANG_WARN_BOOL_CONVERSION = YES; 202 | CLANG_WARN_COMMA = YES; 203 | CLANG_WARN_CONSTANT_CONVERSION = YES; 204 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 205 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 206 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 207 | CLANG_WARN_EMPTY_BODY = YES; 208 | CLANG_WARN_ENUM_CONVERSION = YES; 209 | CLANG_WARN_INFINITE_RECURSION = YES; 210 | CLANG_WARN_INT_CONVERSION = YES; 211 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 212 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 213 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 214 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 215 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 216 | CLANG_WARN_STRICT_PROTOTYPES = YES; 217 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 218 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 219 | CLANG_WARN_UNREACHABLE_CODE = YES; 220 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 221 | CODE_SIGN_IDENTITY = "iPhone Developer"; 222 | COPY_PHASE_STRIP = NO; 223 | DEBUG_INFORMATION_FORMAT = dwarf; 224 | ENABLE_STRICT_OBJC_MSGSEND = YES; 225 | ENABLE_TESTABILITY = YES; 226 | GCC_C_LANGUAGE_STANDARD = gnu11; 227 | GCC_DYNAMIC_NO_PIC = NO; 228 | GCC_NO_COMMON_BLOCKS = YES; 229 | GCC_OPTIMIZATION_LEVEL = 0; 230 | GCC_PREPROCESSOR_DEFINITIONS = ( 231 | "DEBUG=1", 232 | "$(inherited)", 233 | ); 234 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 235 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 236 | GCC_WARN_UNDECLARED_SELECTOR = YES; 237 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 238 | GCC_WARN_UNUSED_FUNCTION = YES; 239 | GCC_WARN_UNUSED_VARIABLE = YES; 240 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 241 | MTL_ENABLE_DEBUG_INFO = YES; 242 | ONLY_ACTIVE_ARCH = YES; 243 | SDKROOT = iphoneos; 244 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 245 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 246 | }; 247 | name = Debug; 248 | }; 249 | B7EBAD251F5871A600A08C26 /* Release */ = { 250 | isa = XCBuildConfiguration; 251 | buildSettings = { 252 | ALWAYS_SEARCH_USER_PATHS = NO; 253 | CLANG_ANALYZER_NONNULL = YES; 254 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 255 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 256 | CLANG_CXX_LIBRARY = "libc++"; 257 | CLANG_ENABLE_MODULES = YES; 258 | CLANG_ENABLE_OBJC_ARC = YES; 259 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 260 | CLANG_WARN_BOOL_CONVERSION = YES; 261 | CLANG_WARN_COMMA = YES; 262 | CLANG_WARN_CONSTANT_CONVERSION = YES; 263 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 264 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 265 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 266 | CLANG_WARN_EMPTY_BODY = YES; 267 | CLANG_WARN_ENUM_CONVERSION = YES; 268 | CLANG_WARN_INFINITE_RECURSION = YES; 269 | CLANG_WARN_INT_CONVERSION = YES; 270 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 271 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 272 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 273 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 274 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 275 | CLANG_WARN_STRICT_PROTOTYPES = YES; 276 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 277 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 278 | CLANG_WARN_UNREACHABLE_CODE = YES; 279 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 280 | CODE_SIGN_IDENTITY = "iPhone Developer"; 281 | COPY_PHASE_STRIP = NO; 282 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 283 | ENABLE_NS_ASSERTIONS = NO; 284 | ENABLE_STRICT_OBJC_MSGSEND = YES; 285 | GCC_C_LANGUAGE_STANDARD = gnu11; 286 | GCC_NO_COMMON_BLOCKS = YES; 287 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 288 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 289 | GCC_WARN_UNDECLARED_SELECTOR = YES; 290 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 291 | GCC_WARN_UNUSED_FUNCTION = YES; 292 | GCC_WARN_UNUSED_VARIABLE = YES; 293 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 294 | MTL_ENABLE_DEBUG_INFO = NO; 295 | SDKROOT = iphoneos; 296 | SWIFT_COMPILATION_MODE = wholemodule; 297 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 298 | VALIDATE_PRODUCT = YES; 299 | }; 300 | name = Release; 301 | }; 302 | B7EBAD271F5871A600A08C26 /* Debug */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 306 | CODE_SIGN_STYLE = Automatic; 307 | INFOPLIST_FILE = App/Info.plist; 308 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 309 | LD_RUNPATH_SEARCH_PATHS = ( 310 | "$(inherited)", 311 | "@executable_path/Frameworks", 312 | ); 313 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TypedNotifications; 314 | PRODUCT_NAME = "$(TARGET_NAME)"; 315 | SWIFT_VERSION = 5.0; 316 | TARGETED_DEVICE_FAMILY = "1,2"; 317 | }; 318 | name = Debug; 319 | }; 320 | B7EBAD281F5871A600A08C26 /* Release */ = { 321 | isa = XCBuildConfiguration; 322 | buildSettings = { 323 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 324 | CODE_SIGN_STYLE = Automatic; 325 | INFOPLIST_FILE = App/Info.plist; 326 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 327 | LD_RUNPATH_SEARCH_PATHS = ( 328 | "$(inherited)", 329 | "@executable_path/Frameworks", 330 | ); 331 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TypedNotifications; 332 | PRODUCT_NAME = "$(TARGET_NAME)"; 333 | SWIFT_VERSION = 5.0; 334 | TARGETED_DEVICE_FAMILY = "1,2"; 335 | }; 336 | name = Release; 337 | }; 338 | /* End XCBuildConfiguration section */ 339 | 340 | /* Begin XCConfigurationList section */ 341 | B7EBAD041F5871A500A08C26 /* Build configuration list for PBXProject "TypedNotifications" */ = { 342 | isa = XCConfigurationList; 343 | buildConfigurations = ( 344 | B7EBAD241F5871A600A08C26 /* Debug */, 345 | B7EBAD251F5871A600A08C26 /* Release */, 346 | ); 347 | defaultConfigurationIsVisible = 0; 348 | defaultConfigurationName = Release; 349 | }; 350 | B7EBAD261F5871A600A08C26 /* Build configuration list for PBXNativeTarget "Example" */ = { 351 | isa = XCConfigurationList; 352 | buildConfigurations = ( 353 | B7EBAD271F5871A600A08C26 /* Debug */, 354 | B7EBAD281F5871A600A08C26 /* Release */, 355 | ); 356 | defaultConfigurationIsVisible = 0; 357 | defaultConfigurationName = Release; 358 | }; 359 | /* End XCConfigurationList section */ 360 | 361 | /* Begin XCSwiftPackageProductDependency section */ 362 | 66C64D4822CA3AE20013B62E /* TypedNotifications */ = { 363 | isa = XCSwiftPackageProductDependency; 364 | productName = TypedNotifications; 365 | }; 366 | 66C64D4C22CA3B0C0013B62E /* TypedNotifications */ = { 367 | isa = XCSwiftPackageProductDependency; 368 | productName = TypedNotifications; 369 | }; 370 | /* End XCSwiftPackageProductDependency section */ 371 | }; 372 | rootObject = B7EBAD011F5871A500A08C26 /* Project object */; 373 | } 374 | -------------------------------------------------------------------------------- /Example/TypedNotifications.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/TypedNotifications.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Joe Fabisevich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "TypedNotifications", 8 | products: [ 9 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 10 | .library( 11 | name: "TypedNotifications", 12 | targets: ["TypedNotifications"]), 13 | ], 14 | dependencies: [ 15 | // Dependencies declare other packages that this package depends on. 16 | // .package(url: /* package url */, from: "1.0.0"), 17 | ], 18 | targets: [ 19 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 20 | // Targets can depend on other targets in this package, and on products in packages which this package depends on. 21 | .target( 22 | name: "TypedNotifications", 23 | dependencies: []), 24 | .testTarget( 25 | name: "TypedNotificationsTests", 26 | dependencies: ["TypedNotifications"]), 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TypedNotifications 2 | 3 | ### A wrapper around `NotificationCenter` for sending typed notifications with payloads across your iOS app. 4 | 5 | [![BuddyBuild](https://dashboard.buddybuild.com/api/statusImage?appID=59a836506532420001f89b3b&branch=master&build=latest)](https://dashboard.buddybuild.com/apps/59a836506532420001f89b3b/build/latest?branch=master) 6 | [![Pod Version](https://img.shields.io/badge/Pod-1.4.0-6193DF.svg)](https://cocoapods.org/) 7 | ![Swift Version](https://img.shields.io/badge/Swift%205.0-brightgreen.svg) 8 | ![License MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg) 9 | ![Plaform](https://img.shields.io/badge/Platform-iOS-lightgrey.svg) 10 | 11 | 12 | ### Asynchronous behavior is hard. Let's make it a little easier so we can turn that frown (🙁) upside down (🙃). 13 | 14 | --- 15 | 16 | Using TypedNotifications is easy. You can drop it into your app and replace all of your un-typed `Notification`s in minutes. 17 | 18 | --- 19 | 20 | ### Registering for Notifications 21 | 22 | You can register notifications for either payload containing notifications, or payload-free notifications. 23 | 24 | ```swift 25 | func register(type: T.Type, observer: Any, object: Any? = nil, selector: Selector) 26 | ``` 27 | 28 | ```swift 29 | func register(type: T.Type, observer: Any, object: Any? = nil, selector: Selector) 30 | ``` 31 | --- 32 | 33 | ### Sending Notifications 34 | 35 | You can send notifications for either payload containing notifications, or payload-free notifications. 36 | 37 | ```swift 38 | func post(typedNotification: T, object: Any? = nil) 39 | ``` 40 | 41 | ```swift 42 | func post(typedNotification: T, object: Any? = nil) 43 | ``` 44 | --- 45 | 46 | ### Extracting values from Notifications 47 | 48 | Only payload containing notifications can have their payload extracted, because, duh. 49 | 50 | ```swift 51 | func getPayload(notificationType: T.Type) -> T.Payload? 52 | ``` 53 | --- 54 | 55 | Now that might look a little scary at first with all those `T`s, but let's break it down with some examples and show you how easy this is. 56 | 57 | ## Examples 58 | 59 | #### Create some values you'd like to send through your app. 60 | 61 | ```swift 62 | enum Job { 63 | 64 | case softwareDeveloper 65 | case designer 66 | case conArtist 67 | 68 | } 69 | 70 | struct Person { 71 | 72 | let name: String 73 | let job: Job 74 | 75 | } 76 | ``` 77 | 78 | #### Create the notification to send your value 79 | 80 | If you have no payload and just want to send a message, use a `TypedNotification` like so. 81 | 82 | ```swift 83 | struct SomeEventNotification: TypedNotification {} 84 | ``` 85 | 86 | For our example, let's use a `TypedPayloadNotification` with a payload though. 87 | 88 | ```swift 89 | struct TypedPersonNotification: TypedPayloadNotification { 90 | 91 | let payload: Person 92 | 93 | } 94 | ``` 95 | 96 | #### Register the notification 97 | 98 | ```swift 99 | NotificationCenter.default.register(type: TypedPersonNotification.self, observer: self, selector: #selector(personNotificationWasReceived)) 100 | ``` 101 | 102 | #### Send the notification 103 | 104 | ```swift 105 | let amanda = Person(name: "Amanda", job: .softwareDeveloper) 106 | let amandaNotification = TypedPersonNotification(payload: amanda) 107 | NotificationCenter.default.post(typedNotification: amandaNotification) 108 | ``` 109 | 110 | 111 | #### And handle the notification 112 | 113 | ```swift 114 | @objc func personNotificationWasReceived(notification: Notification) { 115 | guard let person = notification.getPayload(notificationType: TypedPersonNotification.self) else { 116 | os_log("Could not properly retrieve payload from TypedPersonNotification") 117 | return 118 | } 119 | 120 | let nameText = "Name: \(person.name)" 121 | let jobText = "Job: \(person.job.title)" 122 | 123 | print("Got our Person payload!\n\(nameText)\n\(jobText)") 124 | } 125 | ``` 126 | 127 | ### And that's it! You've sent a typed notification throughout your app. 128 | 129 | If you want to play on expert mode, I recommend using generics and passing notifications through your app that way. 130 | 131 | ```swift 132 | struct GenericTypedPayloadNotification: TypedPayloadNotification { 133 | 134 | let payload: T 135 | 136 | } 137 | ``` 138 | 139 | --- 140 | 141 | ## Requirements 142 | 143 | - iOS 8.0+ 144 | - Xcode 7.3+ 145 | 146 | ## Installation 147 | 148 | SPM will be the default supported installation method from version 1.4.0 and higher, so please integrate by using SPM. 149 | 150 | If you're still using [CocoaPods](http://cocoapods.org/) for version 1.4.0 or below you can install `TypedNotifications` by adding it to your `Podfile`: 151 | 152 | ```ruby 153 | platform :ios, '8.0' 154 | use_frameworks! 155 | 156 | pod 'TypedNotifications' 157 | ``` 158 | 159 | Or install it manually by downloading `TypedNotifications.swift` and dropping it in your project. 160 | 161 | ## About me 162 | 163 | Hi, I'm [Joe](http://fabisevi.ch) everywhere on the web, but especially on [Twitter](https://twitter.com/mergesort). 164 | 165 | ## License 166 | 167 | See the [license](LICENSE) for more information about how you can use TypedNotifications. 168 | -------------------------------------------------------------------------------- /Sources/TypedNotifications/TypedNotification.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | /// A protocol to define notifications that are sent around with our `NotificationCenter` extension functionality. 4 | public protocol TypedNotification {} 5 | 6 | private let userInfoPayloadKey = "_payload" 7 | 8 | /// A protocol to define notifications that are sent around with our `NotificationCenter` extension functionality 9 | /// and contain a payload. 10 | public protocol TypedPayloadNotification: TypedNotification { 11 | 12 | /// The type must be defined a `Notification`. 13 | associatedtype Payload 14 | 15 | /// A payload to send in a notification. It is sent through `Notification`'s the `userInfo` property. 16 | var payload: Payload { get } 17 | } 18 | 19 | public extension NotificationCenter { 20 | 21 | /// This function posts notifications, using a generic parameter tailored to `TypedNotification`s. 22 | /// 23 | /// - Parameter typedNotification: The `TypedNotification` to post. 24 | func post(typedNotification: T, object: Any? = nil) { 25 | let notification = NotificationCenter.generateNotification( 26 | typedNotification: typedNotification, 27 | object: object 28 | ) 29 | self.post(notification) 30 | } 31 | 32 | /// This function posts notifications, using a generic parameter tailored to `TypedPayloadNotification`s. 33 | /// 34 | /// - Parameter typedNotification: The `TypedPayloadNotification` to post. 35 | func post(typedNotification: T, object: Any? = nil) { 36 | let notification = NotificationCenter.generateNotification( 37 | typedNotification: typedNotification, 38 | object: object 39 | ) 40 | self.post(notification) 41 | } 42 | 43 | /// This function registers notifications, tailored to the `TypedNotification` type. 44 | /// 45 | /// - Parameters: 46 | /// - type: The `TypedNotification` type to register. 47 | /// - observer: An observer to use for calling the target selector. 48 | /// - selector: The selector to call the observer with. 49 | func register(type: T.Type, observer: Any, object: Any? = nil, selector: Selector) { 50 | let notificationName = NotificationCenter.generateNotificationName(type: type) 51 | self.addObserver(observer, selector: selector, name: notificationName, object: object) 52 | } 53 | 54 | } 55 | 56 | extension NotificationCenter { 57 | 58 | static func generateNotification(typedNotification: T, object: Any? = nil) -> Notification { 59 | let notificationName = self.generateNotificationName(type: T.self) 60 | return Notification( 61 | name: notificationName, 62 | object: object, 63 | userInfo: nil 64 | ) 65 | } 66 | 67 | static func generateNotification(typedNotification: T, object: Any? = nil) -> Notification { 68 | let notificationName = self.generateNotificationName(type: T.self) 69 | return Notification( 70 | name: notificationName, 71 | object: object, 72 | userInfo: [userInfoPayloadKey : typedNotification.payload] 73 | ) 74 | } 75 | 76 | static func generateNotificationName(type: T.Type) -> Notification.Name { 77 | let name = String(describing: type) 78 | let notificationName = Notification.Name(name) 79 | 80 | return notificationName 81 | } 82 | 83 | } 84 | 85 | public extension Notification { 86 | 87 | /// This function allows you to pull a payload out of a `Notification`, with the result being 88 | /// typed to the defined `Payload` type. 89 | /// 90 | /// - Parameter notificationType: The notificationType to retrieve the payload from. 91 | /// - Returns: The payload from the `TypedNotification`. 92 | func getPayload(notificationType: T.Type) -> T.Payload? { 93 | return self.userInfo?[userInfoPayloadKey] as? T.Payload 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /Tests/TypedNotificationsTests/TypedNotificationTests.swift: -------------------------------------------------------------------------------- 1 | @testable import TypedNotifications 2 | import XCTest 3 | 4 | struct TypedTestNotification: TypedNotification {} 5 | 6 | struct TypedPayloadTestNotification: TypedPayloadNotification { 7 | 8 | let payload: Bool 9 | 10 | } 11 | 12 | 13 | final class TypedNotificationTests: XCTestCase { 14 | 15 | static let passingValue = true 16 | static let failingValue = false 17 | static let testObject = 42 18 | 19 | 20 | // MARK: TypedNotification properties 21 | 22 | let payloadFreeTypedNotification = TypedTestNotification() 23 | 24 | lazy var payloadFreeNotification: Notification = { 25 | return NotificationCenter.generateNotification(typedNotification: self.payloadFreeTypedNotification) 26 | }() 27 | 28 | // MARK: TypedPayloadNotification properties 29 | 30 | let passingTypedPayloadNotification = TypedPayloadTestNotification(payload: TypedNotificationTests.passingValue) 31 | 32 | lazy var passingNotification: Notification = { 33 | return NotificationCenter.generateNotification(typedNotification: self.passingTypedPayloadNotification, object: TypedNotificationTests.testObject) 34 | }() 35 | 36 | let failingTypedPayloadNotification = TypedPayloadTestNotification(payload: TypedNotificationTests.failingValue) 37 | 38 | lazy var failingNotification: Notification = { 39 | return NotificationCenter.generateNotification(typedNotification: self.failingTypedPayloadNotification) 40 | }() 41 | 42 | // MARK: TypedNotification tests 43 | 44 | func testCorrectTypedNotificationGeneration() { 45 | let generatedNotification = NotificationCenter.generateNotification(typedNotification: self.payloadFreeTypedNotification) 46 | XCTAssertTrue(generatedNotification == payloadFreeNotification, "passingTypedNotification was expected to generate a matching notification") 47 | } 48 | 49 | // MARK: NotificationName tests 50 | 51 | func testCorrectNotificationNameGeneration() { 52 | let generatedNotificationName = NotificationCenter.generateNotificationName(type: TypedPayloadTestNotification.self) 53 | XCTAssertTrue(generatedNotificationName == passingNotification.name, "generatedNotificationName was expected to generate a name") 54 | } 55 | 56 | // MARK: TypedPayloadNotification tests 57 | 58 | func testCorrectTypedPayloadNotificationGeneration() { 59 | let generatedNotification = NotificationCenter.generateNotification(typedNotification: self.passingTypedPayloadNotification, object: TypedNotificationTests.testObject) 60 | XCTAssertTrue(generatedNotification == passingNotification, "passingTypedNotification was expected to generate a matching notification") 61 | 62 | guard let testInt = generatedNotification.object as? Int else { 63 | XCTFail() 64 | return 65 | } 66 | 67 | XCTAssertEqual(testInt, TypedNotificationTests.testObject, "passingTypedNotification was expected to generate a matching notification") 68 | } 69 | 70 | func testIncorrectTypedPayloadNotificationGeneration() { 71 | let generatedNotification = NotificationCenter.generateNotification(typedNotification: self.failingTypedPayloadNotification) 72 | XCTAssertFalse(generatedNotification == passingNotification, "failingTypedNotification was expected to generate an incorrect notification") 73 | } 74 | 75 | // MARK: Payload tests 76 | 77 | func testCorrectPayloadExtraction() { 78 | let payload = passingNotification.getPayload(notificationType: TypedPayloadTestNotification.self) 79 | let expectedPayload = TypedNotificationTests.passingValue 80 | XCTAssertTrue(payload == expectedPayload, "passingNotification was expected to carry a payload of true") 81 | } 82 | 83 | func testIncorrectPayloadExtract() { 84 | let payload = failingNotification.getPayload(notificationType: TypedPayloadTestNotification.self) 85 | let expectedPayload = TypedNotificationTests.passingValue 86 | XCTAssertFalse(payload == expectedPayload, "failingNotification was expected to carry a payload of false") 87 | } 88 | 89 | 90 | } 91 | -------------------------------------------------------------------------------- /TypedNotifications.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | spec.name = 'TypedNotifications' 3 | spec.version = '1.3' 4 | spec.license = { :type => 'MIT', :file => 'LICENSE' } 5 | spec.homepage = 'https://github.com/mergesort/TypedNotifications' 6 | spec.authors = { 'Joe Fabisevich' => 'github@fabisevi.ch' } 7 | spec.summary = 'A wrapper around NotificationCenter for sending typed notifications with payloads across your iOS app.' 8 | spec.source = { :git => 'https://github.com/mergesort/TypedNotifications.git', :tag => "#{spec.version}" } 9 | spec.source_files = 'Sources/TypedNotifications/*.swift' 10 | spec.framework = 'Foundation' 11 | spec.requires_arc = true 12 | spec.social_media_url = 'https://twitter.com/mergesort' 13 | spec.ios.deployment_target = '8.0' 14 | spec.swift_version = '5.0' 15 | end 16 | --------------------------------------------------------------------------------