使用 allKeys 时应用程序崩溃,是因为只有一个对象吗?
我不明白为什么
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[__NSCFString allKeys]: unrecognized selector sent to instance 0x9367330'
每当我使用这段代码时
NSDictionary *nextArray = [_classDictionary
valueForKeyPath:@"response.page_records.page_record"];
for (NSDictionary *roster2 in nextArray) {
NSArray *keys = [roster2 allKeys];
if ([keys containsObject:@"title"]){
[pageName addObject:[roster2 objectForKey:@"title"]];
}
if ([keys containsObject:@"id"]) {
[pageID addObject:[roster2 objectForKey:@"id"]];
}
}
xcode都会继续抛出这个错误在尝试调试它之后我发现只有当该键只有一个对象时才会抛出异常。例如当“nextArray”是这样的时候:
(
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = Assignments;
}
)
代码运行时不会出现异常,但是当代码只是简单的时候
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
}
就会抛出异常。为什么会发生这种情况呢?即使只有一个对象,键仍然包含对象标题。为什么抛出此异常还有其他想法吗?和括号有关系吗? (完全猜测)是否有更好的方法来调试到底发生了什么?
编辑:这是 _userDictionary。拥有 objectforKey: @"title" 的有序数组并避免上述错误的最佳方法是什么?
response = {
"status" = ok;
"page_records" = {
"page" = 1;
"page_count" = 1;
"records_per_page" = 50;
"total_record_count" = 2;
"page_record" = (
{
"class_id" = 0001;
"id" = 1234;
"title" = "Test page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = "testing page 2";
}
);
};
};
}
I can't figure out why xcode continues to throw this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[__NSCFString allKeys]: unrecognized selector sent to instance 0x9367330'
whenever I use this code
NSDictionary *nextArray = [_classDictionary
valueForKeyPath:@"response.page_records.page_record"];
for (NSDictionary *roster2 in nextArray) {
NSArray *keys = [roster2 allKeys];
if ([keys containsObject:@"title"]){
[pageName addObject:[roster2 objectForKey:@"title"]];
}
if ([keys containsObject:@"id"]) {
[pageID addObject:[roster2 objectForKey:@"id"]];
}
}
After trying to debug it I found out that the exception is only thrown when there is only one object for the key. For example when "nextArray" is this:
(
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = Assignments;
}
)
the code runs without an exception, but when the code is simply
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
}
The exception is thrown. Why would this be happening? The keys still contains the object title, even if there is only one object. Any other ideas why this exception is being thrown? Does it have something to do with the parenthesis? (total guess) Is there a better way to debug what is really going on?
EDIT: Here is the _userDictionary. What is the best way to have an ordered array of the objectforKey: @"title" and avoid the above error?
response = {
"status" = ok;
"page_records" = {
"page" = 1;
"page_count" = 1;
"records_per_page" = 50;
"total_record_count" = 2;
"page_record" = (
{
"class_id" = 0001;
"id" = 1234;
"title" = "Test page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = "testing page 2";
}
);
};
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在第一个示例中,您正在迭代字典数组,因此正在对数组调用
NSArray *keys = [roster2 allKeys];
。但是,在第二个示例中,您只有一个字典。它不是一个字典数组。因此,您的循环 for (NSDictionary *roster2 in nextArray) 正在迭代字典的键,这些键只是字符串,而不是数组。因此,出现错误。首先,你的命名很混乱。名为
nextArray
的NSDictionary
?试试这个:In your first example, you are iterating through an array of dictionaries, and hence,
NSArray *keys = [roster2 allKeys];
is being called upon an array. However, in your second example, you have just one dictionary. It is not an array of dictionaries. So your loopfor (NSDictionary *roster2 in nextArray)
is iterating through the keys of your dictionary, which are just strings, and not arrays. Hence, the error.First of all, your naming is confusing. An
NSDictionary
namednextArray
? Try this:错误消息包含您需要了解的所有内容:
这意味着您正在尝试在
NSString
实例上调用方法allKeys
。让我们看看在哪里调用
allKeys
:roster2
是 NSString 的实例,而不是 NSDictionary。您期望nextArray
包含NSDictionary
对象,但它却包含NSString
对象。The error message contains everything you need to know:
It means that you're trying to call the method
allKeys
on an instance ofNSString
.Let's look at where you call
allKeys
:roster2
is an instance of NSString, not NSDictionary. You're expectingnextArray
to containNSDictionary
objects, but it instead containsNSString
objects.不,这是因为
NSString
不响应allKeys
。控制台日志告诉您这一点——您需要更仔细地阅读它。在 NeXT 样式的 plist 中,
({})
定义一个包含字典的数组。{}
仅定义一个字典。也就是说,这:在结构上与此不相同:
即使它们包含相同的信息。即使根数组中只有一个元素,您仍然需要告诉 plist 解析器那里有一个数组,因此您需要包含
(
和)
你的逻辑来正确处理它。编辑:鉴于您评论中的信息,解决方案不会那么简单。如果您的源数据是任意的,您需要检查数据的类型以查看它是否是
NSArray
或NSDictionary
,并进行适当的处理。例如:No, it's because
NSString
doesn't respond toallKeys
. The console log tells you this--you need to read it more carefully.In NeXT-style plists,
({})
defines an array containing a dictionary.{}
defines just a dictionary. That is, this:Is not structurally the same as this:
Even though they contain the same information. Even if you have only one element in your root array, you still need to tell the plist parser that there is an array there, so you need to include
(
and)
for your logic to handle it correctly.Edit: Given the information in your comment, the solution will not be quite so simple. If your source data is arbitrary that way, you'll need to check the type of the data to see if it's an
NSArray
orNSDictionary
, and handle it appropriately. For example: