发送到实例的无法识别的选择器[NSCFString subarrayWithRange:] [英] Unrecognized Selector Sent to Instance [NSCFString subarrayWithRange:]

查看:105
本文介绍了发送到实例的无法识别的选择器[NSCFString subarrayWithRange:]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码产生此错误。我无法理解为什么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]);
}

错误:

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

谢谢

推荐答案

这些错误通常与内存管理有关。基本上,您正在向一个现在被其他对象占用的地址发送消息,因为前一个占用者意外地消失了。由于该地址空间可能被任何东西占用,你恰好在问NSCFString它没有响应的东西。

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.

如果你在你之后暂停调试器创建临时数组,你看到分配给temp的是什么?无论-keysArray返回什么,我猜测某些事情并不完全正确。你可能想要仔细检查内存是如何处理的。通过名称,我想你的app委托有一个名为keysArray的数组作为实例变量。也许在创建或分配时没有正确保留?

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?

这篇关于发送到实例的无法识别的选择器[NSCFString subarrayWithRange:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆