gpt4 book ai didi

ios - objective-c : unrecognized selector sent to instance 0x8d75720

转载 作者:行者123 更新时间:2023-11-28 20:03:21 25 4
gpt4 key购买 nike

我正在用 C 语言创建链表,但在添加时出现错误(同时也是警告)。

两者都写在下面。

我尝试了一些方法都无济于事......任何建议都会很棒。

[这是一个最大尺寸的链表!]

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

抛出这个错误

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

这是添加方法

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

发出警告

不兼容的整数到指针的转换将“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

最佳答案

您的 -initWithSize: 方法接受一个指向 NSInteger 的指针,但您试图传入一个 NSInteger 而不是指向一。该方法没有理由采用指针,因为 NSIntegers 适合堆栈并且您不会更改它的值。方法签名可能应该是:

-(id) initWithSize:(NSInteger)size

(当然,您应该在方法中包含 self.size = size;)。这将解决您收到的警告。

至于断言 - 看起来您已经到了列表的末尾。由于您没有包含断言周围的代码,因此无法说明为什么您会得到一个 nil next 指针。

关于ios - objective-c : unrecognized selector sent to instance 0x8d75720,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23076578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com