1

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

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]

2 Answers 2

5

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

NList *list = [[NList alloc] initWithSize:mySize];

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting! Why does it need to be declared in the header? That seems unusual? Shouldn't it become public automatically?
default constructor is not required
@BryanChen For his purposes, I believe it is. He is doing specific initialization. Thankyou for your feedback, though.
without default constructor, the code behave the same. (unless setHead: is implemented with some custom logic) and I can't see any reason to have it.
3

You're a little backwards.

How about:

NList *testList = [[NList alloc] init:SIZE];

where size is the SIZE init you want to use.

Alloc comes before init when you're instantiating Objective-C objects.

3 Comments

NList *testList = [[NList alloc] init:2]; gives me an error No visible @interface for 'NList' declares the selector 'init:' .. = c
Did you forget to add "#import "NList.h"" to the top of whatever .m file you're using NList in?
Still getting No visible @interface for 'NList' declares the selector 'init:' .. yeah I am importing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.