Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 946563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:56:17+00:00 2026-05-15T22:56:17+00:00

i am writing my first custom class in objective-c and when i am trying

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:56:17+00:00Added an answer on May 15, 2026 at 10:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a custom file selection component. In my UI, first the user clicks
Writing my first Linq application, and I'm trying to find the best way to
I'm writing my first Perl app -- an AOL Instant Messenger bot that talks
I am writing my first project that will use autoconf and teaching it to
I'm writing my first PHP app that has to directly deal with dates, and
I am writing a custom Control class in C# for my main project. There're
I am writing a Graph-class using boost-graph-library. I use custom vertex and edge properties
Writing my first silverlight application. I need to deliver some bitmap that the customer
I'm writing my first iPhone app, so I haven't gotten around to figuring out
I am writing my first serious wxWidgets program. I'd like to use the wxConfig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.