我是iphone开发的新手。我正在开发一个应用程序,我想解析这个字典并将电话号码存储在一个数组中。可以任何一个帮助.. 提前谢谢。这是我试过的代码。
-(void)filterContacts{
NSLog(@"I am here");
NSArray *letters = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
NSMutableArray* test=[[NSMutableArray alloc]init];
NSMutableArray* obj=[[NSMutableArray alloc]init];
NSLog(@" Data %@", dataArray);
for (int i=0; i<arrayLetters.count; i++) {
[test addObject:[[dataArray objectAtIndex:0] valueForKey:[arrayLetters objectAtIndex:i]]];
}
for (NSDictionary *dict2 in test) {
NSLog(@"%@",[dict2 valueForKey:@"telephone"]);
}
}
当我打印这个数组时,我得到了null。我必须解析的数据如下所示。
2012-12-03 20:08:29.314(
)
2012-12-03 20:08:29.315 (
"1 (234) 560-789,20313561301"
)
2012-12-03 20:08:29.316 (
"(000) 000-0000"
)
DataArray (
{
"#" = (
);
A = (
);
B = (
);
C = (
);
D = (
);
E = (
);
F = (
);
G = (
);
H = (
);
I = (
);
J = (
);
K = (
{
letter = K;
name = kkkkkk;
rowSelected = "-1";
telephone = "1 (234) 560-789,20313561301";
telephoneSelected = "";
}
);
L = (
);
M = (
);
N = (
);
O = (
);
P = (
);
Q = (
);
R = (
{
letter = R;
name = RRRRR;
rowSelected = "-1";
telephone = "1234567890";
telephoneSelected = "1234567890";
}
);
S = (
);
T = (
);
U = (
);
V = (
);
W = (
);
X = (
);
Y = (
);
Z = (
);
}
)
答案 0 :(得分:1)
你的循环中有错误:
for (NSDictionary *dict in dataArray) {
[test addObject:[[dataArray objectAtIndex:0] valueForKey:[arrayLetters objectAtIndex:i]]];
[obj addObject: [[dataArray objectAtIndex:0]valueForKey:@"telephone"]];
}
注意你是如何迭代NSDictionary的,但是你的循环中没有任何地方你实际上是指你的循环变量。第一行应该引用dict,第二行看起来不对,因为你试图索引字典值而不是字典。试试这个:
for (NSDictionary *dict in dataArray) {
[test addObject:dict valueForKey:[arrayLetters objectAtIndex:i]]];
[obj addObject:[dataArray valueForKey:@"telephone"]];
}
同样,在你的帖子中没有信息的情况下,我在这里做了一些假设。您的原始帖子缺少一些像dataArray格式的详细信息(我假设您的数据样本应该是dataArray的内容,但它有助于明确说明这一点)以及应该是什么arrayLetters。因此,您的代码可能存在其他问题;如果上述问题无法解决您的问题,请修改您的帖子以包含该信息。
答案 1 :(得分:0)
根据您提供的修改代码的详细信息,您没有正确解析字典。检查一下。
-(void)filterContacts{
NSLog(@"I am here");
NSMutableArray* test=[[NSMutableArray alloc]init];
NSMutableArray* obj=[[NSMutableArray alloc]init];
NSLog(@" Data %@", dataArray);
for (int i=0; i<arrayLetters.count; i++) {
for (NSDictionary *dict in dataArray) {
[test addObject:[dict valueForKey:[arrayLetters objectAtIndex:i]]];
if([test objectAtIndex:0]!= nil){
[obj addObject: [[[dict valueForKey:[arrayLetters objectAtIndex:i]] objectAtIndex:0]valueForKey:@"telephone"]];
}
}
// NSLog(@" My test %@", obj);
}
修改强>
-(void)filterContacts{
NSLog(@"I am here");
NSArray *letters = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
NSMutableArray* test=[[NSMutableArray alloc]init];
NSMutableArray* obj=[[NSMutableArray alloc]init];
NSLog(@" Data %@", dataArray);
for (int i=0; i<arrayLetters.count; i++) {
[test addObject:[[dataArray objectAtIndex:0] valueForKey:[arrayLetters objectAtIndex:i]]];
}
for (NSDictionary *dict2 in test) {
NSLog(@"%@",[[dict2 objectAtIndex:0] valueForKey:@"telephone"]);
}
}
在dict2中,值为
(
{
letter = K;
name = kkkkkk;
rowSelected = "-1";
telephone = "1 (234) 560-789,20313561301";
telephoneSelected = "";
}
);
因此您应该显示objectAtIndex:0
以识别字典中的telephone
键。
Note check for the character "K" or "R" only coz all other character values are empty