Table of Contents Previous Section

Objective-C Classes in the Registration Application

The Registration application includes the Person and RegistrationManager classes.

Person Class

When users enter data in the Main page of the Registration application, the data is stored in an NSDictionary that's used to initialize an instance of the Person class. The Person class includes a validate method that's used to check whether the data entered by the user includes values for a name and address. The validate method returns an NSDictionary. This dictionary contains a status message and a validation flag that indicates whether the registration should be allowed to proceed. If the user failed to enter a name or address, the validation flag value is "No," which disallows the registration. The status message then prompts the user to supply the missing information.

The Person class also includes the name and personAsDictionary methods. The name method is simply used to return the Person's name, while the method personAsDictionary returns a dictionary representation of the Person. The dictionary representation is used when the Person's data is written out to a file in a property list format (this is described in more detail in the section on the RegistrationManager class, below).

The header (.h) and implementation (.m) files for the Person class are listed below.

Person.h
// Person.h
#import <foundation/NSObject.h>
@class NSDictionary, NSString;
@interface Person:NSObject {
   NSDictionary *personRecord;
}
+ personWithDictionary:(NSDictionary *)personDict;
- initWithDictionary:(NSDictionary *)personDict;
- (NSDictionary *)validate;
- (NSString *)name;
- (NSDictionary *)personAsDictionary;
@end
Person.m
// Person.m

#import "Person.h"
#import <WebObjects/WebObjects.h>
@implementation Person 
+ personWithDictionary:(NSDictionary *)personDict
{
  return [[[[self class] alloc]
      initWithDictionary:personDict]
      autorelease];
}
- initWithDictionary:(NSDictionary *)personDict
{
  [super init];
  personRecord = [personDict copy];
  return self;
}
- (void)dealloc
{
  [personRecord release];
  [super dealloc];
}
- (NSDictionary *)validate 
{
  NSMutableDictionary *isValid = [NSMutableDictionary dictionary];
    
   if(![[personRecord objectForKey:@"address"] length] &&
          ![[personRecord objectForKey:@"name"] length]){
    [isValid setObject:@"You must supply a name and address."
      forKey:@"failureReason"];
        [isValid setObject:@"No" forKey:@"isValid"];
    }
    else if (![[personRecord objectForKey:@"name"] length]){
        [isValid setObject:@"You must supply a name."
      forKey:@"failureReason"];
        [isValid setObject:@"No" forKey:@"isValid"];
    }
    else if(![[personRecord objectForKey:@"address"] length]){
        [isValid setObject:@"You must supply an address."
      forKey:@"failureReason"];
        [isValid setObject:@"No" forKey:@"isValid"];
    }

    else {
        [isValid setObject:@"Yes" forKey:@"isValid"];
    }
    return isValid;
}
- (NSString *) name
{
  return [personRecord objectForKey:@"name"];
}
- (NSDictionary *) personAsDictionary
{
  return personRecord;
}
@end

RegistrationManager Class

The RegistrationManager class has two primary functions: it registers a new Person (which entails writing the Person's data out to the People.array file), and it returns an array containing all of the registrants.

The header (.h) and implementation (.m) files for the RegistrationManager class are listed below.

RegistrationManager.h
// RegistrationManager.h
#import <foundation/NSObject.h>
@class Person, NSMutableArray, NSArray;
@interface RegistrationManager:NSObject {
   NSMutableArray *registrants;
}
+ manager;
- init;
- (NSDictionary *)registerPerson:(Person *)newPerson;
- (NSArray *)registrants;
@end
RegistrationManager.m
// RegistrationManager.h
#import <WebObjects/WebObjects.h>
#import "RegistrationManager.h"
#import "Person.h"
@implementation RegistrationManager 

+ manager
{
   return [[[[self class] alloc] init] autorelease];
}
- init
{
  NSString *path = [WOApp pathForResource:@"People" ofType:@"array"];
  [super init];
  registrants = [[NSMutableArray arrayWithContentsOfFile:path] retain];  
  if (!registrants)
    registrants = [[NSMutableArray alloc] init];
  
  return self;
}
- (void)dealloc {
  [registrants release];
  [super dealloc];
}
- (NSDictionary *)registerPerson:(Person *)newPerson 
{
  int i;
  NSDictionary *results;
  NSString *currentName, *newPersonName = [newPerson name];
  NSString *path = [WOApp pathForResource:@"People" ofType:@"array"];
 
  results = [newPerson validate];
  if ([[results objectForKey:@"isValid"] isEqual:@"No"])
    return results;
  for (i = [registrants count]-1; i >= 0; i--) {
    currentName = [[registrants objectAtIndex:i] objectForKey:@"name"];
      if ([currentName isEqual:newPersonName]) {
            [registrants removeObjectAtIndex:i];
          break;
      }
  }
  [registrants addObject:[newPerson personAsDictionary]];
  [registrants writeToFile:path atomically:YES];
  return results;
}
// Return an array of all registrants
- (NSArray *)registrants
{
  return registrants;
}
@end

RegistrationManager's init and registerPerson: methods use the WOApplication method pathForResource:ofType: to load the file People.array into the application. This method takes a path and the file's extension as arguments:

NSString *path = [WOApp pathForResource:@"People" ofType:@"array"];

You can use this method to load different kinds of resources into your application---for example, images, sound files, data files, and so on.

Another noteworthy feature of RegistrationManager is its use of an NSArray data source. The reason that the instance variable registrants object can be initialized from the file People.array is because the file contains data in an property list format. A property list is a compound data type that consists of NSStrings, NSArrays, NSDictionaries, and NSDatas. Property lists can be represented in an ASCII format, and property list objects such as NSDictionaries and NSArrays can consequently be initialized from ASCII files that use this format. The file People.array contains an NSArray of NSDictionaries.

Table of Contents Next Section