Assert statement in Objective C

Hi,
if you like to use assertions in your source code, but you miss this feature in ObjectiveC, give a look to this small class.
Assert.h

  1. #import <Foundation/Foundation.h>
  2. @interface Assert : NSObject {
  3. }
  4. +(void)that:(BOOL)expr;
  5. @end

Assert.m

  1. #import "Assert.h"
  2. @implementation Assert
  3. +(void)that:(BOOL)expr{
  4.  if (!expr) {
  5.   [NSException raise:@"Failed assertion." format:@"Failed assertion", nil];
  6.  }
  7. }
  8. @end

As you can see, there’s only one static method that you can use wherever you want.
In this snippet here’s an example how to use it.

  1. #import "Assert.h"
  2. -(void)doSomething{
  3.  [Assert that:(<your_assertion>)];
  4. }

One thought on “Assert statement in Objective C

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>