Nov 3, 2010

Catgories in Objective-C

Today I want to give a short example on how to use Categories in Objective-C to extend an existing class.
Let's say that you want to access safely to an array containing 'n' objects, but you don't want every time you access it to check if the index is contained into the array count.

A possible solution is to use a Category to extend the NSArray class.

Create a new project and add to it a new .h and .m files. Let call them NSArray+SafeAccess.h and NSArray+SafeAccess.m.
Inside the NSArray+SafeAccess.h put the declaration of the new Category:

@interface NSArray (SafeAccess) 

- (id)objectSafeAtIndex:(NSUInteger)index;

@end

Then in the NSArray+SafeAccess.m file write the implementation of the new method that will extend the NSArray with the safe access without obtaining a crash for out of bounds access:

@implementation NSArray (SafeAccess)

- (id)objectSafeAtIndex:(NSUInteger)index {
    return (index >= [self count]) ? nil : [self objectAtIndex:index];
}

@end

Later on in your code just import the NSArray+SafeAccess.h and you can use the new safe access method.
Try these lines of code:

#import "NSArray+SafeAccess.h"

...

NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:@"First"];
[arr addObject:@"Second"];
[arr addObject:@"Third"];

id obj = [arr objectSafeAtIndex:3];
assert(obj == nil);
obj = [arr objectSafeAtIndex:2];
assert(obj == @"Third");
[arr release];

Hope you enjoy with this simple example.
Comments and suggestions are welcome :)

Aug 9, 2010

Ecological Footprint

Dear All,
today I wanna present you the new update v1.1 for Eco Footprint, the iPhone application dedicated to the ecology that allows you to calculate in a simple but effective way your footprint on nature.
The updates are some bug fixing that allow the application to run on the new iOS4 and the adoption of the Apple iAd network.
The application make use of Core Data layer to query and store values on sqlite database.

Below you can se a video of the application that show how to use it to calculate the footprint.



The result can be shared with your friends through your Facebook account.
The application can be downloaded for free at the Apple Store.

Please leave your comments on the application to suggest improvements or changes to make to the app even better and usable.

Thanks for reading :)
Enhanced by Zemanta

Apr 17, 2010

Augmented Reality on iPhone

Hi all, I'm back after a long absence from the blog.
Today I wanna share with you the presentation I made for the University of Bologna on the development of Augmented Reality application for the iPhone 3Gs.

Enjoy the show :)


Reblog this post [with Zemanta]

Jan 16, 2010

Locate your position using GPS

For this test application you need to create a new View-Based Application project (I called it TestLocation).
Once created, open the TestLocationViewController.xib file by double clicking on it. drag over it four labels and a button to make start the update location..
After the editing the result should be like in the following image:




Save the xib file and go to the TestLocationViewController.h and add two IBOutlets to be connected to the empty labels and an action

 - (IBAction)startUpdate:(id)sender 

for the button. These labels will show the latitude and the longitude of your GPS position, while the button will start the location manager. Connect the action to the Touch Up Inside event of the button.

Now comes the location code; in your project right click on the TestLocation target and select the 'Get Info'. In the General tab add the CoreLocation.framework as linked library.

Now come back in the TestLocationViewController.h and add the inclusion of the location framework, the protocol CLLocationManagerDelegate and a member variable that define the location manager (of type CLLocationManager).
I defined the location manager as a property, so it will be instantiated only when needed.

Now switch to the .m file and implement the location protocol delegate methods: didUpdateToLocation (called on success of updating GPS position) and didFailWithError (called if an error occours, for example no GPS signal is present).

Inside the method didUpdateToLocation you have to extract the new position and update the label's text using the following code:

    CLLocationCoordinate2D loc = newLocation.coordinate;
    [lblLatitude setText:[NSString stringWithFormat:@"%f", loc.latitude]];
    [lblLongitude setText:[NSString stringWithFormat:@"%f", loc.longitude]];

then stop updating the location manager:

    [locationManager stopUpdatingLocation];

If you implemented the location manager as property, redefine the accessor method to check if it has been already created, otherwise create it, set the desired accuracy and the delegate as follows:

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.delegate = self;

Now you are ready; Press the Build and Go button and when you'll press the button, the location manager will start to update the GPS position. The simulator comes with a fake GPS positions, so you'll be able to see something in the labels.
The complete project can be downloaded from here.

Enjoy and thanks for reading!