bezzoon
bezzoon

Reputation: 2019

objective-c : unrecognized selector sent to instance 0x8d75720

I am creating a Linked List in C and I am getting an error upon addition (and also a warning).

Both of which are written below.

I have tried a few things to no avail...any advice would be amazing.

[it is a linked list with a max size!]

//in the tester
XCTAssertNotNil(testList.head.next);

throws this error

 failed: ((testList.head.next) != nil) failed: throwing "-[__NSCFNumber next]: unrecognized selector sent to instance 0x8d75720"

this is the add method

- (void)add:(NSObject *)node {
    Node *newNode = [[Node alloc] init];
    if (self.head)
        newNode.next = self.head;
    else
        self.head = newNode;
    self.num_nodes++;
}
 

NList *testList = [[NList alloc] initWithSize:2];

gives a warning

Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSInteger *' (aka 'int *')

This is the constructor

@property (nonatomic,readwrite) NSInteger size;

.....

- (id) initWithSize:(NSInteger *)size {
    self = [super init];
    if (self){
        self.head = nil;
        self.size = *size;
        self.num_nodes = 0;
    }
    return self;
}

http://pastebin.com/SpW75Pf0

edit

- (void)testAdd
{
    NList *testList = [[NList alloc] initWithSize:2];
    NSObject *testNodeOne =  @1;
    [testList add:(testNodeOne)];
    XCTAssertNotNil(testList.head);
    NSObject *testNodeTwo =  @3;

    [testList add:testNodeTwo];
    XCTAssertNotNil(testList.head);
    XCTAssertNotNil(testList.head.next);

}

head.next throws the error

/LinkedListTest.m: test failure: -[LinkedListTest testAdd] failed: ((testList.head.next) != nil) failed: throwing "-[__NSCFNumber next]: unrecognized selector sent to instance 0x8d75720"

=c

Upvotes: 0

Views: 241

Answers (1)

user1118321
user1118321

Reputation: 26395

Your -initWithSize: method takes a pointer to an NSInteger, but you are trying to pass in an NSInteger rather than a pointer to one. There's no reason for the method to take a pointer, since NSIntegers fit on the stack and you aren't changing its value. The method signature should probably be:

-(id) initWithSize:(NSInteger)size

(and of course, you should have self.size = size; inside the method). That will fix the warning you're getting.

As for the assertion - it looks like you're hitting the end of the list. Since you didn't include the code surrounding the assertion, it's impossible to tell why you're getting a nil next pointer.

Upvotes: 3

Related Questions