March Madness Day 1

It was proposed at FUBAR Labs that for each day in March, everyone (well, anyone who wanted to participate) should write a program. I was on the fence about it, but decided to do one at the last hour since I had been telling myself that I should do more programming. So, here’s mine for Day 1. It’s a “simple” hello world program that uses objects written in Objective C.

ComplexHelloWorld.m:

#import <Foundation/Foundation.h>

#import "Person.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Person *eric = [[Person alloc] initWithName:@"Eric"];
    Person *world = [[Person alloc] init];

    [eric printHello];
    [world printHello];

    [pool drain];
    return 0;
}

Person.h:

#import <Cocoa/Cocoa.h>

@interface Person : NSObject {
    NSString *myName;
}

- (id)initWithName: (NSString *)name;
- (void)printHello;

@end

Person.m:

#import "Person.h"

@implementation Person

- (id)init {
    return [self initWithName:@"World"];
}

- (id)initWithName:(NSString *) name {
    [super init];
    myName = [[NSString alloc] initWithString:name];
    [myName retain];

    return self;
}

- (void)dealloc {
    [myName dealloc];
    [super dealloc];
}

- (void)printHello {
    NSLog(@"Hello, %@!", myName);
}

@end

It’s pretty simple first program (mainly because I decided to do this at the last hour). It probably leaks memory all over the place because I don’t remember how the hell to do memory management in Objective C (all the autorelease pool stuff is default Xcode template).

Let’s see if I can keep this up.

0 Responses to “March Madness Day 1”


  • No Comments

Leave a Reply