-2

I have a array with objects A,B,A,A,B with keys 1,2,3,4,5

but as A is repeated NSDictionary is not taking 'A' 2 times and gives error saying

-[NSDictionary initWithObjects:forKeys:]: count of objects (1) differs from count of keys (0)'

So whats the solution for this

My Code:

Ar=[[NSArray alloc]initWithObjects:A,A,A,A,A,nil];
Ar2=[[NSArray alloc]initWithObjects:1,2,3,4,5,nil];
    dic2=[[NSMutableDictionary alloc]initWithObjects:Ar forKeys:Ar2];
4

3 回答 3

1

使用新语法使代码更具可读性,并且在大多数情况下可以避免此类问题

NSDictionary *data = @{@"1" : @"A", @"2" : @"B", @"3" : @"A", @"4" : @"A", @"5" : @"B"};
于 2013-01-31T12:37:03.263 回答
1

请尝试下面的代码片段

NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
 [data setValue:@"A" forKey:@"1"];
 [data setValue:@"B" forKey:@"2"];
 [data setValue:@"A" forKey:@"3"];
 [data setValue:@"A" forKey:@"4"];
 [data setValue:@"B" forKey:@"5"];
于 2013-01-31T12:32:16.563 回答
0

应该是这样的。

    NSArray *Ar=[[NSArray alloc]initWithObjects:@"A",@"A",@"A",@"A",@"A",nil];
NSArray *Ar2=[[NSArray alloc]initWithObjects:@1,@2,@3,@4,@5,nil];
NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjects:Ar forKeys:Ar2];

此致。

于 2013-01-31T13:40:07.503 回答