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.