Assert statement in Objective C

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

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


  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. }

Good environment variable values during iPhone development

Directly from GHUnit, a small note about which environment variables to set while developing for the iPhone.

Environment Variable:                 Default:  Set to:
NSDebugEnabled                           NO       YES
NSZombieEnabled                          NO       YES
NSDeallocateZombies                      NO       NO (or YES)
NSHangOnUncaughtException                NO       YES
NSAutoreleaseFreedObjectCheckEnabled     NO       YES

Actually, I keep forgetting them, so I hope this will be useful to me.

Here an example that shows how to set those environment variables.

Setting iPhone development variables in XCode

iPhone screen dimensions

This picture can be useful to those of you that develop graphics targeted to the iPhone platform. Here you see the dimensions in pixel of an iPhone windows in some of the most common configurations. Click on the image to enlarge it.

iphone dimensions

Chosing the number of decimal digits when printing a number with iPhone

This is a small snippet, useful whenever you want to convert a floating point number in a string with a fixed number of decimal digits.

NSNumberFormatter *format = [NSNumberFormatter new];
 [format setMaximumFractionDigits:1];
 NSNumber *aNumber = [[NSNumber alloc ] initWithDouble:54.4235264];
 NSString label = [[NSString alloc] initWithFormat:@"The number is %@", [format stringForObjectValue:aNumber]];
 [distance release];
 [format release];

Obviously the NSNumberFormatter class offers plenty of other method you can use to customize the way your numbers should be rendered.

iPhone: how to select a UITabBarItem programmatically

I’m working to an iPhone application with a TabBar on the footer area. Among the others there are a “Home” button and an “Account” button. The first allows the user to see a view that gives an overview of the application. The latter shows the view that the user uses to manage his/her account.

If the user is not currently logged in, in the “Home” view a warning message is displayed. There is also a button that is supposed to show the “Account” view. In that way it’s more clear for the user that he/she should log in.

How can you accomplish that task ? That post shows you the way I followed.

The big idea is to retrieve the TabBarController from the “Home” controller, and programmatically change the current view. To do that, the “Home” controller should get a reference to the TabBarController. But you can easily set that dependency in Interface Builder. You only need to provide a suited IBOutlet to the Home controller.

That is the tree structure of the controllers and views, as built in the interface builder.


So, that’s an excerpt of the HomeController showing you only what is really needed to reach our goal. The goToRegistrationPage method will execute programmatically the switch to the account page.

@interface HomeViewController : UIViewController {
UITabBarController *mainTabBarController;
@property(nonatomic, retain) IBOutlet UITabBarController *mainTabBarController;
-(IBAction) goToRegistrationPage;

Now, we only need to set in Interface Builder the link between the the IBOutlet we just created and the Tab Bar Controller. Here’s the result.

iphone_change_tabbar_programmatically_1As you can see the mainTabBarController outlet is linked to the Tab Bar Controller we want to manage programmatically. That’s not difficult at all. Let’s give a look to the goToRegistrationPage method.

-(IBAction) goToRegistrationPage {

Quite easy, isn’t it ?

Now the user click on the button on the Home view and he/she get exactly the same result of touching the tab bar button in the footer.

Hope you can find it useful.