我有一个包含对象A,B,A,A,B的数组,其中包含键1,2,3,4,5
但是当A重复时,NSDictionary没有采取'A'2次并且提出错误说
-[NSDictionary initWithObjects:forKeys:]: count of objects (1) differs from count of keys (0)'
那么解决方案是什么
我的代码:
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];
答案 0 :(得分: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"];
答案 1 :(得分:1)
使用新语法使代码更具可读性,并且在大多数情况下可以避免此类问题
NSDictionary *data = @{@"1" : @"A", @"2" : @"B", @"3" : @"A", @"4" : @"A", @"5" : @"B"};
答案 2 :(得分: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];
最诚挚的问候。