无法识别的选择器发送到实例 [NSCFString subarrayWithRange:]

发布于 2024-09-26 09:34:08 字数 738 浏览 8 评论 0原文

我有以下代码会产生此错误。我不明白为什么 subarrayWithRange 消息被发送到字符串?当它明显是一个数组时?

static const int kItemsPerView = 20;
NSRange rangeForView = NSMakeRange( page * kItemsPerView, kItemsPerView );

NSMutableArray *temp = [[APP_DELEGATE keysArray] mutableCopyWithZone:NULL]; 
NSArray *itemsForView = [temp subarrayWithRange:rangeForView];

for (int loopCounter = 0;loopCounter < r*c;loopCounter++){
    NSLog(@"%i: %@ ", loopCounter, [itemsForView objectAtIndex:loopCounter]);
}

错误:

-[NSCFString subarrayWithRange:]: unrecognized selector sent to instance 0x6b071a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [NSCFString subarrayWithRange:]:

谢谢

I have the following code which is producing this error. I cannot understand why the subarrayWithRange message is being sent to a string? When it is clearly an array?

static const int kItemsPerView = 20;
NSRange rangeForView = NSMakeRange( page * kItemsPerView, kItemsPerView );

NSMutableArray *temp = [[APP_DELEGATE keysArray] mutableCopyWithZone:NULL]; 
NSArray *itemsForView = [temp subarrayWithRange:rangeForView];

for (int loopCounter = 0;loopCounter < r*c;loopCounter++){
    NSLog(@"%i: %@ ", loopCounter, [itemsForView objectAtIndex:loopCounter]);
}

Error:

-[NSCFString subarrayWithRange:]: unrecognized selector sent to instance 0x6b071a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [NSCFString subarrayWithRange:]:

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-10-03 09:34:08

此类错误通常与内存管理相关。本质上,您正在向一个地址发送一条消息,该地址现在已被其他对象占用,因为之前的占用者意外消失了。由于该地址空间可能被任何东西占用,因此您碰巧向 NSCFString 询问了一些它不会响应的内容。

如果在创建临时数组后立即暂停调试器,您会看到分配给临时的内容是什么?我猜测 -keysArray 返回的内容不太正确。您可能需要仔细检查内存在应该返回的内容中是如何处理的。从名称来看,我想您的应用程序委托有一个名为“keysArray”的数组作为实例变量。也许在创建或分配时没有正确保留它?

These kinds of errors are usually memory-management-related. Essentially, you're sending a message to an address that's now occupied by some other object because the previous occupant has unexpectedly disappeared. Since that address space could be occupied by anything, you just happen to be asking an NSCFString something to which it doesn't respond.

If you pause the debugger right after you create the temp array, what do you see assigned to temp? I'm guessing something's not quite right with whatever -keysArray returns. You might want to double-check how the memory is handled in whatever that's supposed to return. By the name, I suppose your app delegate has an array called "keysArray" as an instance variable. Perhaps that's not being properly retained when it's created or assigned?

驱逐舰岛风号 2024-10-03 09:34:08

所以我就有了这个。我做了一件蠢事。我将 UITextView 分配给一个字符串而不是它的文本属性。即:

myObj.txtbxThing = [NSString stringWithFormat:@"%@", stuffString];

而不是:

myObj.txtbxThing.text = [NSString stringWithFormat:@"%@", stuffString];

So I had this one. I did something stupid. I assigned the UITextView to a string instead of it's text property. ie:

myObj.txtbxThing = [NSString stringWithFormat:@"%@", stuffString];

instead of:

myObj.txtbxThing.text = [NSString stringWithFormat:@"%@", stuffString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文