0

我是 iphone 开发的新手。我正在开发一个应用程序,我想解析这本字典并将电话号码存储在一个数组中。任何人都可以帮助..thanx 提前。这是我尝试过的代码。

-(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"]);
}

}

当我打印这个数组时,我得到了空值。我必须解析的数据如下所示。

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 =         (
    );
}

)
4

2 回答 2

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 应该是什么。因此,您的代码可能存在其他问题;如果上述方法不能解决您的问题,请编辑您的帖子以包含该信息。

于 2012-12-03T13:56:01.980 回答
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

于 2012-12-03T13:59:47.437 回答