I’ve made some good progress on Fort Collins for Kids since my last post. As you can see, it’s changed dramatically:

OK, so maybe it hasn’t changed dramatically. It looks identical to the last screen shot, and in terms of functionality, it doesn’t do anything new. Under the hood though, it’s a different story. The first version used an array of text strings to populate the list, while the second has a small class heirarchy underneath.

The first class is Attraction, a model class that represents a thing to do with kids in Fort Collins. At this point, it’s just a collection of properties wrapped up in a class, with an initializer to create the object.

@interface Attraction : NSObject
{
    NSString *name;
    NSString *type;
    BOOL inside;
    NSString *description;
    NSString *link;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *type;
@property (nonatomic) BOOL inside;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *link;

- (Attraction *)initWithName:(NSString*) n 
        AndType:(NSString*) t 
    AndInside:(BOOL)i 
    AndDescription:(NSString*)d 
    AndLink:(NSString *)l;
@end

Those are not the final list of properties, but it’s enough to get started with. The class doesn’t have any persistance capabilities- that will come later.

The second class is an AttractionStore, a singleton which provides a collection of Attraction objects.

@interface AttractionStore : NSObject
{
    NSMutableArray *allAttractions;
}

+ (AttractionStore *)defaultStore;

- (NSArray *)allAttractions;
- (Attraction *)createAttractionWithName: (NSString *)n 
    AndType:(NSString *) t 
    AndInside:(BOOL)i 
    AndDescription:(NSString *)d 
    AndLink:(NSString *)l;
@end

The + sign on the defaultStore method indicates that this is a Class method. This provides access to the singleton from client code, which looks like this.

AttractionStore *myStore = [AttractionStore defaultStore];

In the implementation there are a few more methods to help make AttractionStore a singleton:

static AttractionStore *defaultStore = nil;

@implementation AttractionStore

+ (AttractionStore *)defaultStore
{
    if (!defaultStore) {
        defaultStore = [[super allocWithZone:NULL] init];
    }
    return defaultStore;
}

//Prevent creation of additional instances
+ (id)allocWithZone:(NSZone *)zone
{
    return [self defaultStore];
}

- (id)init
{
    //If we already have an instance of AttractionStore...
    if (defaultStore) {
        //return the old one.  
        return defaultStore;
    }
    self = [super init];
    if (self) {
        allAttractions = [[NSMutableArray alloc] init];
    }
    return self;
}

This should be mostly self-explanatory. The defaultStore class method returns the existing AttractionStore object, creating and initializing a new instance if one doesn’t exist already. There are a few more methods needed to handle Objective C reference counting:

- (id)retain
{
    //do nothing
    return self;
}
- (oneway void)release
{
    //do nothing
}
- (NSUInteger)retainCount
{
    return NSUIntegerMax;
}

These routines prevent the singleton from being released. This of course means that the attractionStore will take up memory until the application ends. I’ll have to keep an eye on this object and make sure it doesn’t grow too big. I should note that this code is almost an exact copy of similar code in The Big Nerd Ranch Guide.

With all this groundwork out of the way, the ViewController just creates the AttractionStore, and populates it with a few hard-coded values:

- (void)createAttractionStore
{
    AttractionStore *attractionStore = [AttractionStore defaultStore];

    [attractionStore createAttractionWithName:@"Cool Beans"  
         AndType:@"Indoor Play Center" 
         AndInside:TRUE
         AndDescription:@"description here"    
         AndLink:@"http://www.coolbeansplayhouse.com/"];
    //etc.
}

The ViewController holds onto the array that it uses to populate the table view:

attractions = [[AttractionStore defaultStore] allAttractions];

There’s two obvious short-comings of the code as it currently exists:
– There’s no way to view the data! The UITableView just lists the names.
– The list is still hard-coded.

And now you know the next two items in the backlog.