Enforcing that a class posts a particular NSNotification?


Is there any way to ensure that a class posts a particular NSNotification?

(I have a set of classes, and I would like to enforce at compile-time (if possible) that the class posts a required NSNotification).

Alternatively, if that is not possible, is there any workaround?


It's fundamentally impossible to predict at compile time what will happen at run time. The closest you can get is static analysis, but even that can't predict anything that happens outside of your own code, such as inside Foundation.

You can, however, do this with unit tests, since the test runner actually runs the code under test.

You'll need to create a test bundle target, if you haven't already. Your target will use SenTestingKit to run your tests, which you create. (On the iPhone, you'll also need Google Toolbox for, uh, Mac. They have <a href="http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting" rel="nofollow">a handy tutorial on using GTM for iPhone tests</a>.)

You'll create a SenTestCase subclass to test whether your real object posts a notification. It'll look something like this:

@interface FrobnitzerNotificationsTest: SenTestCase { BOOL frobnitzerDidCalibrate; } - (void) frobnitzerDidCalibrate:(NSNotification *)notification; @end @implementation FrobnitzerNotificationsTest - (void) testFrobnitzerCalibratePostsNotification { Frobnitzer *frobnitzer = …; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(frobnitzerDidCalibrate:) name:FrobnitzerDidCalibrate object:frobnitzer]; frobnitzerDidCalibrate = NO; //This should post a notification named FrobnitzerDidCalibrate with the receiver as the object. [frobnitzer calibrate]; //If it did, our notification handler set frobnitzerDidCalibrate to YES (see below). [nc removeObserver:self name:FrobnitzerDidCalibrate object:frobnitzer]; STAssertTrue(frobnitzerDidCalibrate, @"Frobnitzer did not post a notification when we told it to calibrate"); } - (void) frobnitzerDidCalibrate:(NSNotification *)notification { frobnitzerDidCalibrate = YES; } @end

You'll need one instance variable and one notification-handler method for every notification you want to test for, and one test method for every method you want to test for notifications.

Also, if using GTM, you must substitute GTMSenTestCase for SenTestCase above.


  • Java: Using generics to implement a class that operates on different kinds of numbers
  • How to read POST Request data in PHP sent from AngularJS
  • How to connect from Linux to Android Bluetooth socket
  • Extend a regular expression to negative number
  • Turn off Zurb Foundation 5 meta tags
  • Possible F# type inference limitation
  • TFS Build notification options not populating build definitions
  • How to run a single row of a Cucumber scenario outline example table in RubyMine?
  • Swift 3 Custom extension of ns measurement? Ex. Sheeps to goats
  • Objective-C: When to know that you are abusing the SIngleton method of Global Variables
  • Display Current Video in Windows Phone 8 using AudioVideoCaptureDevice?
  • TFS edit build log using custom activity
  • Proguard Exception java.io.IOException: Duplicate zip entry
  • Find tangent points on a curve from a user-given point outside the curve
  • Defining a method whose return type is the singleton type of an argument of that method
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • python: forcing relative imports to search from script file
  • Plotting densities in R
  • Getting media player state in windows phone 7
  • Git describe fails to return most recent annotated tag
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Asynchronous UI Testing in Xcode With Swift
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • Deselecting radio buttons while keeping the View Model in synch
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • angularjs unit test when to use $rootScope.$new()
  • Perl system calls when running as another user using sudo
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal