如何从 NSDictionary 获取整个对象
How to Get Entire object from NSDictionary
这是我的数据。
NSDictionary *dictResponse = [[NSDictionary alloc]init];
//Here is dictResponse value is
(
{
"created_on" = "0000-00-00 00:00:00";
id = 627;
"modified_on" = "0000-00-00 00:00:00";
name = "";
"user_id" = 99;
},
{
"created_on" = "2016-05-06 14:43:45";
id = 625;
"modified_on" = "2016-05-06 14:43:45";
name = Ggg;
"user_id" = 99;
},
{
"created_on" = "2016-05-03 17:21:52";
id = 623;
"modified_on" = "2016-05-03 17:21:52";
name = Qwerty;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:38";
id = 601;
"modified_on" = "2016-04-29 20:12:38";
name = Teat2;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
},
{
"created_on" = "2016-05-09 13:04:00";
id = 626;
"modified_on" = "2016-05-09 13:04:00";
name = Testios;
"user_id" = 99;
})
现在我想访问完整的一组对象,即
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
}
来自我的 dictResponse
所以当我使用
dictResponse[0] 或 dictResponse1 .. 出现错误,那么如何从 NSDictionary 中检索整个集合?
请检查以下控制台日志输出。
您不能使用索引(如 dictResponse[0])访问字典中的对象。字典中的对象没有索引,它们有键。要从字典中获取对象,您可以使用 objectForKey 方法:
NSObject* myObject = [dictResponse objectForKey:@"my_key"];
你可以这样做:
id arrResponse = dictResponse;
if ([arrResponse isKindOfClass:[NSArray class]] && arrResponse != nil && arrResponse != (id)[NSNull null])
{
NSArray *arrResults = arrResponse;
//if you want retrieve specific entries then here is the code for that
for (int i = 0 ; i<arrResults.count; i++) {
NSString *strName = [[arrResults objectAtIndex:i]valueForKey:@"name"];
NSString *strCreated_on = [[arrResults objectAtIndex:i]valueForKey:@"created_on"];
NSString *strModified_on = [[arrResults objectAtIndex:i]valueForKey:@"modified_on"];
NSInteger userID = [[arrResults objectAtIndex:i]valueForKey:@"user_id"];
}
}
你的问题没有给出足够的细节,所以以下是猜测:
so when I use dictResponse[0]
or dictResponse[1]
.. am getting error
你永远不会说你遇到了什么错误。我猜你会遇到 compile 错误而不是执行错误?
你似乎知道你的代码是错误的,就像你写的评论一样:
Yes I know this is not valid dictionary value but it is possible to get something like that value in NSDictionary, so question is not wrong, yes my response is array of dictionary. Means from console log I can give dictResponse[0] and it prints first part as expected.
确切地说,您可以打破类型系统并将数组引用存储到一个类型为保存字典引用的变量中,但这样做不是一个好主意!
当编译器看到 dictResponse[0]
时,它在句法上将其识别为数组索引操作,因此它会在 dictResponse
上查找执行数组索引的方法。它对 dictResponse
的所有了解,并记住这是在编译器时间 发生的 ,就是你已经声明它是一个 NSDictionary *
,所以它会查找数组索引NSDictionary
支持的方法,但找不到,给出错误:
Expected method to read an array element not found on object of type 'NSDictionary *'
尝试正确键入您的变量并查看您的代码是否有效。
HTH
这是我的数据。
NSDictionary *dictResponse = [[NSDictionary alloc]init];
//Here is dictResponse value is
( {
"created_on" = "0000-00-00 00:00:00";
id = 627;
"modified_on" = "0000-00-00 00:00:00";
name = "";
"user_id" = 99;
},
{
"created_on" = "2016-05-06 14:43:45";
id = 625;
"modified_on" = "2016-05-06 14:43:45";
name = Ggg;
"user_id" = 99;
},
{
"created_on" = "2016-05-03 17:21:52";
id = 623;
"modified_on" = "2016-05-03 17:21:52";
name = Qwerty;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:38";
id = 601;
"modified_on" = "2016-04-29 20:12:38";
name = Teat2;
"user_id" = 99;
},
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
},
{
"created_on" = "2016-05-09 13:04:00";
id = 626;
"modified_on" = "2016-05-09 13:04:00";
name = Testios;
"user_id" = 99;
})
现在我想访问完整的一组对象,即
{
"created_on" = "2016-04-29 20:12:27";
id = 600;
"modified_on" = "2016-04-29 20:12:27";
name = Test1;
"user_id" = 99;
}
来自我的 dictResponse
所以当我使用 dictResponse[0] 或 dictResponse1 .. 出现错误,那么如何从 NSDictionary 中检索整个集合?
请检查以下控制台日志输出。
您不能使用索引(如 dictResponse[0])访问字典中的对象。字典中的对象没有索引,它们有键。要从字典中获取对象,您可以使用 objectForKey 方法:
NSObject* myObject = [dictResponse objectForKey:@"my_key"];
你可以这样做:
id arrResponse = dictResponse;
if ([arrResponse isKindOfClass:[NSArray class]] && arrResponse != nil && arrResponse != (id)[NSNull null])
{
NSArray *arrResults = arrResponse;
//if you want retrieve specific entries then here is the code for that
for (int i = 0 ; i<arrResults.count; i++) {
NSString *strName = [[arrResults objectAtIndex:i]valueForKey:@"name"];
NSString *strCreated_on = [[arrResults objectAtIndex:i]valueForKey:@"created_on"];
NSString *strModified_on = [[arrResults objectAtIndex:i]valueForKey:@"modified_on"];
NSInteger userID = [[arrResults objectAtIndex:i]valueForKey:@"user_id"];
}
}
你的问题没有给出足够的细节,所以以下是猜测:
so when I use
dictResponse[0]
ordictResponse[1]
.. am getting error
你永远不会说你遇到了什么错误。我猜你会遇到 compile 错误而不是执行错误?
你似乎知道你的代码是错误的,就像你写的评论一样:
Yes I know this is not valid dictionary value but it is possible to get something like that value in NSDictionary, so question is not wrong, yes my response is array of dictionary. Means from console log I can give dictResponse[0] and it prints first part as expected.
确切地说,您可以打破类型系统并将数组引用存储到一个类型为保存字典引用的变量中,但这样做不是一个好主意!
当编译器看到 dictResponse[0]
时,它在句法上将其识别为数组索引操作,因此它会在 dictResponse
上查找执行数组索引的方法。它对 dictResponse
的所有了解,并记住这是在编译器时间 发生的 ,就是你已经声明它是一个 NSDictionary *
,所以它会查找数组索引NSDictionary
支持的方法,但找不到,给出错误:
Expected method to read an array element not found on object of type 'NSDictionary *'
尝试正确键入您的变量并查看您的代码是否有效。
HTH