-1
这是代码。这是nsdictionary的奇怪问题
self.names = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
NSMutableArray *subArray = [[NSMutableArray alloc] init];
NSMutableArray *arrayOfKeys = [[NSMutableArray alloc] init];
NSMutableDictionary *bufDict = [[NSMutableDictionary alloc] init];
NSString *bufStr;
NSString *newKeyStr;
NSString *oldKeyStr;
oldKeyStr = @"";
newKeyStr = oldKeyStr;
NSLog(@"start making NEW format");
for (int i=0; i < [temp count]; i++)
{
//if (!(bufDict == nil)) [bufDict removeAllObjects];
bufDict = [temp objectAtIndex:i];
bufStr = [bufDict objectForKey:kNameKey];
if ([bufStr length]>=2)
{
if ([newKeyStr isEqualToString:oldKeyStr])
{
}
else
[arrayOfKeys addObject:newKeyStr];
oldKeyStr = newKeyStr;
}
}
int x=0;
for (int i = 0; i< [arrayOfKeys count];i++)
{
newKeyStr = [arrayOfKeys objectAtIndex:i];
for (int j=x; j< [temp count]; j++)
{
bufDict = [temp objectAtIndex:j];
bufStr = [bufDict objectForKey:kNameKey];
if ([bufStr length] >=2)
{
oldKeyStr = [bufStr substringToIndex:1];
if ([oldKeyStr isEqualToString:newKeyStr])
{
[subArray addObject:bufDict];
}
else
{
NSLog(@"newKEyStr setting to mainDict is %@",newKeyStr);
[mainDict setObject:subArray forKey:newKeyStr];
[self.names setObject:subArray forKey:newKeyStr];
NSArray *arr= [self.names objectForKey:newKeyStr];
NSLog(@"array count: %i",[arr count]);
[subArray removeAllObjects];
x=j;
break;
}
}
}
}
NSLog(@"end of making format dict!!!");
[mainDict release];
self.keys = arrayOfKeys;
[arrayOfKeys release];
[subArray release];
[bufDict release];
for (int v = 0 ; v< [keys count];v++)
{
NSString *str = [keys objectAtIndex:v];
NSLog(@"str = %@",str);
NSArray *arr = [self.names objectForKey:str];
NSLog(@"arr= %i",[arr count]);
}
所以我的日志
start making NEW format
newKEyStr setting to mainDict is 1
array count: 1
newKEyStr setting to mainDict is 2
array count: 22
newKEyStr setting to mainDict is 3
array count: 2
newKEyStr setting to mainDict is A
array count: 12
newKEyStr setting to mainDict is B
array count: 16
newKEyStr setting to mainDict is C
array count: 33
newKEyStr setting to mainDict is D
array count: 6
newKEyStr setting to mainDict is E
array count: 9
newKEyStr setting to mainDict is F
array count: 6
newKEyStr setting to mainDict is G
array count: 5
newKEyStr setting to mainDict is H
array count: 17
newKEyStr setting to mainDict is I
array count: 3
end of making format dict!!!
str = 1
arr= 1
str = 2
arr= 1
str = 3
arr= 1
str = A
arr= 1
str = B
arr= 1
str = C
arr= 1
str = D
arr= 1
str = E
arr= 1
str = F
arr= 1
str = G
arr= 1
str = H
arr= 1
str = I
arr= 1
正如你可以看到它看起来像我在nsarrays但最终以阵列的NSDictionary self.names有一些nsdictionaries当我要检查日志说nsarray中只有一个对象。有人能帮助我吗? 对不起,但我无法解决问题几个小时。谢谢你
什么是'temp'? – ayoy 2012-03-03 13:02:06
@ayoy temp是nsarray的官方代码 – 2012-03-03 13:06:46
有几件事情:1)通过'init'将'bufDict'泄露,然后分配给var,最好使用ARC。 2)你可以使用autoreleased版本的初始'初始化变量,然后消除释放,更好地使用ARC。 3)你可以通过使用“快速枚举和更清洁/更少的代码,例如:for(NSDictionary * bufDict in temp)'来消除索引变量'i'和'v'。清理代码(重构)将让它更容易理解它发生了什么 ' – zaph 2012-03-03 13:48:32