在objective-c中使用alloc/init或new时自定义类未实现自身

在objective-c中使用alloc/init或new时自定义类未实现自身,objective-c,Objective C,我正在用objective-c编写我的第一个自定义类,当我尝试实现自定义类的一个实例时,我得到警告,说“car可能不响应'+alloc'”和“car可能不响应'+init'”,如果我使用new,我会得到同样的警告,它可能不响应new。有人知道为什么会这样吗?这是我的密码: #import <Foundation/Foundation.h> @interface Car { NSString *color; NSString *make; int year; } -

我正在用objective-c编写我的第一个自定义类,当我尝试实现自定义类的一个实例时,我得到警告,说“car可能不响应'+alloc'”和“car可能不响应'+init'”,如果我使用new,我会得到同样的警告,它可能不响应new。有人知道为什么会这样吗?这是我的密码:

#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;
}
#导入
@接口车
{
NSString*颜色;
NSString*制造;
国际年;
}
-(作废)打印;
-(void)setColor:(NSString*)c;
-(void)setMake:(NSString*)m;
-(无效)设定年份:(int)y;
@结束
@实施车
-(作废)打印
{
NSLog(@“福特的$d%@是$@.”,年份,品牌,颜色);
}
-(void)setColor:(NSString*)c
{
颜色=c;
}
-(void)setMake:(NSString*)m
{
make=m;
}
-(无效)设定年份:(int)y
{
年份=y;
}
@结束
int main(int argc,const char*argv[]{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
汽车*汽车;
汽车=[car alloc];
car=[car init];
[汽车设置颜色:@“蓝色”];
[汽车制造商:@“Bronco”];
[车辆设置年份:1992年];
[汽车印刷];
[车辆释放];
[泳池排水沟];
返回0;
}

看起来您有两个问题;首先,您可能应该显式地将NSObject子类化。第二,您没有在分配的内存上调用init。。。尝试以下更改:

@interface Car : NSObject
您还应该考虑使用“setColor”、“setMake”等的属性,因为您没有正确地保留或释放这些字符串,它们将泄漏并造成难看的混乱。它真的帮助我打开了静态分析器(你可以在项目设置中设置它,或者在启用分析器的情况下按Apple-Shift-A进行构建)

编辑:

好的,所以被接受的“答案”帖子有内存问题。。。而且它看起来不会被修复,所以这里是整个事情。。。妥善处理:


#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;
}
您可以对我发布的代码使用完全相同的main方法,它可以正常工作,因为Car类将保留字符串,并在完成后释放它。(并且setXXX:在使用属性时也有效)


编辑:当我尝试使用静态字符串演示EXEC_BAD_访问时,编译器比我聪明。。。哈。

首先,您的Car类应该从NSObject继承(这就是找不到+alloc方法的原因)

@接口车:NSObject { NSString*颜色; NSString*制造; 国际年; }

  • (作废)打印
  • (void)setColor:(NSString*)c
  • (void)setMake:(NSString*)m
  • (无效)设定年份:(int)y
@结束

“init”方法是一种实例方法,您只能将该消息发送给对象,而不能发送给类(Car)。通常的方法是向类发送+alloc消息以实例化该类的对象。然后,通过-init消息发送此创建的对象:

汽车*汽车

汽车=[caralloc]; car=[carinit];

或者最方便的方式:

car=[[car alloc]init]


alloc方法创建对象并为其保留内存,因为它是类方法,因为还没有对象。然后将init消息发送到创建的对象。

@thyrgle;我想他还没到那个地步。除了格式说明符,他还有几个更重要的问题,比如他的内存管理。

#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;
}


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;
}

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

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

Car *car;

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

car = [[Car alloc] init];