I am getting an issue cannot init a class object even after adding lines to the constructor such as
我得到一个问题,即使在向构造函数添加行之后也无法初始化类对象
self = [super init];
or
self = [[super init] alloc];
And I am not sure what to do.
我不知道该怎么做。
This is the specific error:
这是具体的错误:
file:///%3Cunknown%3E: test failure: -[LinkedListTest testAdd] failed: *** +[NList<0x8e14> init]: cannot init a class object.
.m
@interface NList()
@property (weak, nonatomic, readwrite) NSObject *head;
@property (nonatomic,readwrite) NSInteger *size;
@end
@implementation NList
@synthesize size = _size;
- (id) init:(NSInteger *)size {
//is this even necessary? I don't want object methods.. or do I ?
if (self){
_head = nil;
_size = size;
}
return self;
}
.h
@interface NList : NSObject
@property (nonatomic,readonly) NSInteger *size;
@property (weak, readonly, nonatomic) NSObject *head;
- (void)add:(NSObject *)node;
@end
test class
- (void)testAdd
{
NList *testList = [[NList init] alloc];
// Card *testCardOne = [[Card init] alloc];
// [testList add:(testCardOne)];
XCTAssertNotNil(testList.head);
}
I have tried adding the line
我试过添加该行
self = [[super init] alloc];
to the constructor to no avail.
对构造函数无济于事。
No visible interfacce for nlist declares
nlist声明没有可见的接口
or self = [super init]
complains cannot init a class object!
抱怨无法初始化一个类对象!
EDIT
I realized that it is not asking me for the size! the constructor requires a size parameter...how do I do this! Ahh [looks up docs]
我意识到这不是问我的大小!构造函数需要一个size参数......我该怎么做!啊[查找文档]
2 个解决方案
#1
3
A few things.
一些东西。
You need a default constructor
您需要一个默认构造函数
- (id)init {
self = [super init];
if (self) {
self.head = nil;
}
return self;
}
Now that you have a default constructor (that calls the superclasses constructor), you need a more specific one for your purposes.
既然您有一个默认构造函数(调用超类构造函数),您需要一个更具体的构造函数用于您的目的。
- (id)initWithSize:(int)size {
self = [self init]; // sets head, calls super constructor.
if (self) {
self.size = size;
}
return self;
}
Edit: Note, the last one had to be in your .h
file so it is visible. And also, when instantiating this class, call
编辑:注意,最后一个必须在您的.h文件中,以便它可见。而且,在实例化这个类时,请调用
NList *list = [[NList alloc] initWithSize:mySize];
NList * list = [[NList alloc] initWithSize:mySize];
#2
3
You're a little backwards.
你有点倒退了。
How about:
NList *testList = [[NList alloc] init:SIZE];
where size is the SIZE init you want to use.
其中size是您要使用的SIZE init。
Alloc comes before init when you're instantiating Objective-C objects.
当您实例化Objective-C对象时,Alloc在init之前出现。
#1
3
A few things.
一些东西。
You need a default constructor
您需要一个默认构造函数
- (id)init {
self = [super init];
if (self) {
self.head = nil;
}
return self;
}
Now that you have a default constructor (that calls the superclasses constructor), you need a more specific one for your purposes.
既然您有一个默认构造函数(调用超类构造函数),您需要一个更具体的构造函数用于您的目的。
- (id)initWithSize:(int)size {
self = [self init]; // sets head, calls super constructor.
if (self) {
self.size = size;
}
return self;
}
Edit: Note, the last one had to be in your .h
file so it is visible. And also, when instantiating this class, call
编辑:注意,最后一个必须在您的.h文件中,以便它可见。而且,在实例化这个类时,请调用
NList *list = [[NList alloc] initWithSize:mySize];
NList * list = [[NList alloc] initWithSize:mySize];
#2
3
You're a little backwards.
你有点倒退了。
How about:
NList *testList = [[NList alloc] init:SIZE];
where size is the SIZE init you want to use.
其中size是您要使用的SIZE init。
Alloc comes before init when you're instantiating Objective-C objects.
当您实例化Objective-C对象时,Alloc在init之前出现。