【发布时间】:2010-07-25 01:57:07
【问题描述】:
我正在用 Objective-C 编写我的第一个自定义类,当我尝试实现我的自定义类的实例时,我收到警告说“汽车可能无法响应 '+alloc'”和“汽车可能无法响应'+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;
}
【问题讨论】:
标签: objective-c