这是nsdictionary的奇怪问题

时间:2012-03-03 12:46:29

标签: ios nsarray 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

你可以看到它看起来像我有nsdictionary self.names与数组和nsarrays中的一些nsdictionaries但最后当我想检查LOG时说nsarray中只有一个对象。有人能帮助我吗? 很抱歉这个问题很长,但几个小时后我无法解决问题。谢谢你

1 个答案:

答案 0 :(得分:0)

所以我和我的朋友谈过,他帮我解决了这个问题。这是正确的代码!

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    for(NSDictionary *item in self.temp)
    {
        NSString *name = [item objectForKey:@"name"];
        if ([name length] > 1)
        {
            NSString *firstLetter = [[name substringToIndex:1] uppercaseString];
            NSMutableArray *itemArray = [dict objectForKey:firstLetter];
            if(!itemArray)
            {
                itemArray = [NSMutableArray array];
                [dict setObject:itemArray forKey:firstLetter];
            }
            [itemArray addObject:item];
        }
    }