2014-04-15 36 views
0

我在C中創建一個鏈接列表,並且在添加(並且是警告)時出現錯誤。objective-c:無法識別的選擇器發送到實例0x8d75720

這兩個都寫在下面。

我已經嘗試了幾件事情無濟於事...任何建議都會很棒。

[它是一個鏈表用最大的尺寸!]

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

拋出這個錯誤

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

這是add方法

- (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]; 

給出警告

Incompatibl È整數指針轉換髮送 'INT' 到類型的參數 'NSInteger的*'(又名 'INT *')

這是構造

@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

編輯

- (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拋出錯誤

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

= C

+0

第一個告訴'testList.head'返回'NSCFNumber',它沒有名爲'next'的屬性 – Rugmangathan

+1

只是讓你知道如果你不共享你所有的代碼,沒有人會幫助。我無法訪問pastebin,所以我不會幫助,這很簡單。另外這與'xcode IDE'無關。 – Popeye

回答

3

-initWithSize:方法需要一個指向NSInteger,但是你想在NSInteger傳遞,而不是一個指針之一。該方法沒有理由採取指針,因爲NSIntegers適合堆棧,並且不會更改其值。該方法的簽名大概應該是:

-(id) initWithSize:(NSInteger)size 

(當然,你應該有self.size = size;方法內)。這將解決您收到的警告。

至於聲明 - 它看起來像是你打到了列表的最後。由於你沒有包含斷言的代碼,因此不可能說明爲什麼你得到一個零指針next

+0

我在pastebin鏈接上有代碼!當我做你現在說的我得到...類型實現'initWithSize:':'NSInteger *'(aka'int *')vs'NSInteger'(aka'int')通過構造函數的方法聲明! – bezzoon

+0

如果你不在這裏發佈你的代碼,其他人將來無法理解這個問題和答案。至於警告,當然你也必須改變方法原型。這聽起來像是你錯過了這一步。 – user1118321

+0

好吧!我添加了斷言代碼!你對方法原型是正確的。我在那裏檢查,但我沒有注意到問題..我想是時候睡覺了,而不是代碼..哇。思想幫助更多與其他錯誤? – bezzoon

相關問題