我正在尝试创建一个类的实例Dog
,但是编译器无法识别该类并且不会实例化它。Pet
我尝试将初始化程序从超类复制到Dog
中,但这并没有奏效。为什么我不能实例化Dog
对象?
宠物.h
#import <Foundation/Foundation.h>
@interface Pet : NSObject
{
// Some ivars
}
//Randomizer
+(id)randomPet;
//Initializer
-(id)initName:(NSString *)name
initColor:(NSString *)color
initBreed:(NSString *)breed;
// Getters and setters...
@end
宠物
#import "Pet.h"
@implementation Pet
+(id)randomPet
{
// Empty
}
-(id)initName:(NSString *)name
initColor:(NSString *)color
initBreed:(NSString *)breed;
{
self = [super init];
if(self)
{
// Initialization...
}
return self;
}
@end
狗.h
#import "Pet.h"
@interface Dog : Pet
-(id)initName:(NSString *)name initColor:(NSString *)color
initBreed:(NSString *)breed;
// Getters and setters...
@end
狗.m
#import "Dog.h"
@implementation Dog
+(id)RandomPet
{
// Snip...
}
- (id)initName:(NSString *)name
initColor:(NSString *)color
initBreed:(NSString *)breed;
{
self = [super init];
if(self) {
// Initialization...
}
return self;
}
主文件
#import <Foundation/Foundation.h>
#import "Pet.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Dog *d = [[Dog alloc] init]];
}
return 0;
}