iPhone Development: Custom Classes

Code example
Sep
14

Sooner or later you will have the need to be able to store data in your own custom construct. Creating your own classes in Cocoa is quite different to PHP or Java, but it's still fairly easy once you get used to the little nuances in the Cocoa language.

One of the first things I wanted to know how to do after learning how to use Cocoa's implementation of Views and Controllers was how to create my own class and how to use it in an application. When handling data I'm used to having a class that has an attribute for each field in a table and then member functions for performing actions on a singleton so ideally I wanted to do the same for accessing data using Core Data or data held in a SQLLite database. In writing this quick and easy "how-to" I've tried to think of how to approach it from the point of view of a PHP or .NET developer to aid in people trying to "jump ship" from PHP to Cocoa.

To start with make sure that you've got the Classes folder in your project selected and then select "New File..." from the "File" menu. This will open a new window where you can decide what sort of file it is you want to add to your project. We just want a standard Cocoa Touch class so under the iPhone OS group choose "Cocoa Touch Class" and then click "Objective-C class". You can then choose if you want it to be a subclass of NSObject, UITableViewCell, or UIView. In this case we want it to be a subclass of NSObject so choose that and then click Next. You will then have to type in the name for your new class file and click the Next button once more - in this case I have named the class "Trip".

You will now have two new files in your Classes folder - if you named the class the same as me you'll have Trip.h and Trip.m in this folder. If you've coded in C++ or any variation on the language then you're likely to already know what the difference between the two files are. If you're new to any form of the C language then all you'll need to know for now is that the .h file is a "header" file, and the .m file is for implementing the class and it's functions. The following is an example of a header file for the Trip class.

#import <Foundation/Foundation.h>

@interface Trip : NSObject {
   NSNumber *trip_id;
   NSString *trip_name;
   NSDate *trip_start_date;
   NSDate *trip_end_date;
   NSString *trip_location;
}

- (id) initWithId:(NSNumber *) new_id
      name:(NSString *) new_name
      start_date:(NSDate *) new_start_date
      end_date:(NSDate *) new_end_date
      location:(NSString *) new_location;

- (NSMutableArray *) getAllTrips;

- (void) dealloc;

@property (nonatomic, copy) NSNumber *trip_id;
@property (nonatomic, copy) NSString *trip_name;
@property (nonatomic, copy) NSDate *trip_start_date;
@property (nonatomic, copy) NSDate *trip_end_date;
@property (nonatomic, copy) NSString *trip_location;

So here we are saying that the interface Trip is an extension of the NSObject class and that have defined it as having a selection of properties of different types. If you're familiar with other OO languages such as C++ you're likely to already understand that prefixing a variable with an asterisk (*) means that it is a pointer even though in some languages it's represented by an ampersand.

We then specify three functions: the first of these we'll treat as a constructor method - it will pass back a Trip object with values set in each of the properties. The id in parentheses represents the return type, and by specifying this as an id we are specifying that it is a member function that will return itself. In Objective-C parameters are listed after the function name quite differently to what you'd expect to see in languages such as PHP. The first parameter we just specify a type and then a variable name for the parameter to be accessed with, the subsequent parameters then have names fields which must be specified in order.

The second function I will use to return multiple Trip items as an NSMutableArray - in a future tutorial I'll show you how to populate this with results from Core Data or SQLLite, but later in this example we'll just return a static array instead. Finally we move on to the last function which we use to deallocate the resources used by the class to help the Cocoa Touch application with it's memory management (as it doesn't clean up afterwards for you).