Objective C: How to Write a Instantiating Custom Init

Viewed 30859

Very basic question, but I have an error in my code that can only be answered by one assumption: my class isn't being instantiated!

I haven't written much in Objective C in some time, and I was never really good, so please point out even the most painfully obvious.

I am using:

ObjectSelectionViewController *length = [[ObjectSelectionViewController alloc] initWithMeasureType:0];
ObjectSelectionViewController *mass = [[ObjectSelectionViewController alloc] initWithMeasureType:1];
ObjectSelectionViewController *volume = [[ObjectSelectionViewController alloc] initWithMeasureType:2];

NSLog(@"%@", [length measurementType]);
NSLog(@"%@", [mass measurementType]);
NSLog(@"%@", [volume measurementType]);

The NSLogs return whichever measurement was assigned last, regardless of the separate allocs and inits.

Here is the constructor of the ObjectSelectionViewController class:

#import "ObjectSelectionViewController.h"

@implementation ObjectSelectionViewController

NSString *measurementType;

-(ObjectSelectionViewController*) initWithMeasureType:(int)value
{
switch (value) {
    case 0: // Length
        measureType = @"Length";
        break;

    case 1: // Mass
        measureType = @"Mass";
        break;

    case 2: // Volume
        measureType = @"Volume";
        break;
}
return self;
}

-(NSString*) measurementType
{
return measureType;
}

Thanks for the help, it's driving me crazy!

5 Answers
Related