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.

iphone_change_tabbar_programmatically_0

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 {
 mainTabBarController.selectedIndex=3;
}

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.

Readonly computed properties in Objective-C

Do you need to provide your Objective-C class with a read-only computed property ? Not a big task… just define the property in the .h file as usually, remembering to¬† specify “readonly” among the attributes. In the .m implementation file you won’t use the @synthesize keyword to automatically generate the getter and the setter methods of the property as you probably usually do. Instead you’ll write the getter method. This allows you to write the code to compute the property.

Here’s an example, don’t take it as an example of good programming, it’s only to demonstrate the use of read-only computed properties.

The Booking class has a date property. This is awkwardly not a NSDate, but a NSString. This is because the date is actually loaded from an XML service and the original programmer choose to store it as a NSString. Thus, we’d like to have a nice NSDate property, standing by the NSString property. Here the fact is that we cannot add another NSDate property in the standard way, to avoid the two dates be unrelated. What we really need is that the NSDate property would be computed from the string version of the date. Thus, we need a property whose value is computed starting from the value of another property and not stored in an instance variable as we’re used to do.

So, here’s the code… first of all the .h file

#import <Foundation/Foundation.h>

@interface Booking : NSObject {
 NSString *date;
}

@property(nonatomic,retain) NSString *date;
@property(nonatomic,readonly) NSDate *nsDate;

@end

As you can see, we declared the read-only property “nsDate” but there is no any instance variable to store it. Let’s go ahead with the .m implementation file.

#import "Booking.h"
#import "Conversion.h"

@implementation Booking

@synthesize date;

-(NSDate*)nsDate{
 //Here an utility method is used to convert the date from NSString to NSDate.
 return [Conversion toNSDate:date];
}

@end

So, here’s the trick. Just write the code that computes the value of the property in the getter method.