Joe
Joe

Reputation: 1344

custom class not implementing itself when i use alloc/init or new in objective-c

i am writing my first custom class in objective-c and when i am trying to implement an instance of my custom class i get warnings that say "car may not respond to '+alloc'" and "car may not respond to '+init'" and if i use new instead i get the same warning that it may not respond to new. does anyone know why this might be? here is my code:

#import <Foundation/Foundation.h>

@interface Car
{
    NSString *color;
    NSString *make;
int year;
}

- (void) print;
- (void) setColor: (NSString *) c;
- (void) setMake: (NSString *) m;
- (void) setYear: (int) y;


@end

@implementation Car

- (void) print
{
    NSLog(@"The $d Ford %@ is $@.", year, make, color);
}

- (void) setColor: (NSString *) c
{
    color=c;
}

- (void) setMake: (NSString *) m
{
    make=m;
}

- (void) setYear: (int) y
{
    year=y;
}


@end



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

    Car *car;

    car = [Car alloc];
    car = [Car init];

    [car setColor:@"blue"];
    [car setMake:@"Bronco"];
    [car setYear:1992];

    [car print];
    [car release];

    [pool drain];
    return 0;
}

Upvotes: 1

Views: 870

Answers (2)

Steve
Steve

Reputation: 31642

Looks like you have two problems; one, you should probably explicitly subclass NSObject. Two, you aren't calling init on the allocated memory... try the following changes:

@interface Car : NSObject
Car *car = [[Car alloc] init];

You should also look into using properties for your "setColor", "setMake", etc. because you aren't retaining or releasing those strings properly, and they will leak and cause an ugly mess. It really helped me to turn on the static analyzer (you can set this in the project settings, or press Apple-Shift-A to build with the analyzer enabled).

EDIT:

OK, so the accepted "answer" post has memory issues... and it doesn't look like it's going to get fixed, so here is the whole thing... properly done:


#import <Foundation/NSObject.h>
#import <Foundation/Foundation.h>

@interface Car : NSObject
{
    NSString *color;
    NSString *make;
    int year;
}

@property (retain,readwrite) NSString * color;
@property (retain,readwrite) NSString * make;
@property (assign,readwrite) int year;

- (void) print;

@end

@implementation Car

@synthesize color;
@synthesize make;
@synthesize year;

- (void) print
{
    NSLog(@"The %d Ford %@ is %@.", year, make, color);
}

- (void) dealloc
{
    if (color)
        [color release];

    if (make)
        [make release];

    [super dealloc];
}

@end



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

    Car *car = [[Car alloc] init];

    car.color = @"blue";
    car.make = @"Bronco";
    car.year = 1992;

    [car print];
    [car release];

    [pool drain];
    return 0;
}

If you want to see the PROBLEM with the accepted answer, try this modified main function (using the rest of his post):



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

    Car *car = [[Car alloc] init];

    // Let's pretend we are getting a string from somewhere else:
    NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardInput];
    NSData *inputData;
    NSString *inputString;
    printf("Type the color: ");
    inputData = [fileHandle availableData];
    inputString = [[NSString alloc] initWithData: inputData encoding:NSUTF8StringEncoding];

    [car setColor:inputString];
    [car setMake:@"Bronco"];
    [car setYear:1992];

    // This works:
    [car print];

    // But now, the place that gave us the string releases it:
    [inputString release];

    // UT OH! Danger Will Robinson!
    [car print];

    [car release];

    [pool drain];
    return 0;
}

You can use the exact same main method with my posted code, and it will work without an error because the Car class will retain the string, and release it when it's done. (and the setXXX: is also valid when using properties)

EDIT: Compiler outsmarted me when I tried to demo the EXEC_BAD_ACCESS using a static string... ha.

Upvotes: 2

Tobi
Tobi

Reputation: 5519

First of all, your Car-class should inherit from NSObject (thats the reason why the +alloc method isnt found)

@interface Car : NSObject { NSString *color; NSString *make; int year; }

  • (void) print;
  • (void) setColor: (NSString *) c;
  • (void) setMake: (NSString *) m;
  • (void) setYear: (int) y;

@end

The "init" method is an instance-method that meens you can only send that message to an object not to a class (Car). The normal way is to send the +alloc message to the class to instanciate an object of that class. then you send this created object the -init message:

Car *car;

car = [Car alloc]; car = [car init];

Or the most convenient way:

car = [[Car alloc] init];

The alloc method creates the object and reserves the memory for it, for that its a CLASS-method because there is no object yet. the init message is then send to the created object.

Upvotes: 1

Related Questions