tags:

views:

49

answers:

2

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;
}
+3  A: 

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.

Steve
@thyrgle; I don't think he's at that point yet. He has a couple more important problems beyond just the format specifiers, like his memory management.
Steve
I am beaten... +1. :)
thyrgle
A: 
Tobi