无法识别的选择器发送到实例 [NSCFString subarrayWithRange:]
我有以下代码会产生此错误。我不明白为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此类错误通常与内存管理相关。本质上,您正在向一个地址发送一条消息,该地址现在已被其他对象占用,因为之前的占用者意外消失了。由于该地址空间可能被任何东西占用,因此您碰巧向 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?
所以我就有了这个。我做了一件蠢事。我将 UITextView 分配给一个字符串而不是它的文本属性。即:
而不是:
So I had this one. I did something stupid. I assigned the UITextView to a string instead of it's text property. ie:
instead of: