2012-03-03 3 views
-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 

당신은 내가 LOG는 말한다 검사 할 때 내가 nsarrays에서하지만 마지막에있는 NSDictionary 배열과 self.names 거기에 몇 가지 nsdictionaries을 가지고있는 것처럼 보이는 볼 수 있듯이이다 nsarray에는 오직 하나의 객체 만 있습니다. 누군가 나를 도울 수 있습니까? 긴 질문에 대해 유감스럽게 생각하지만 몇 시간 동안 문제를 해결할 수는 없습니다. 감사합니다

+0

'임시'란 무엇입니까? – ayoy

+0

@ayoy temp는 dictinaries가있는 nsarray입니다. –

+0

몇 가지 : 1)'init'으로'bufDict'를 누출 한 다음, var에 할당하면 ARC를 사용하는 것이 좋습니다. 2) 초기'init'-ed vars의 autoreleased 버전을 사용하고 해제를 제거하면 ARC를 사용하는 것이 더 좋습니다. 3) "빠른 열거 형을 사용하여 색인 vars'i'와'v'를 제거 할 수 있으며, 예 :'(NSDictionary * bufDict in temp)'에 대해서도 클리너/코드가 적습니다. 그 일이 무엇인지 쉽게 이해할 수있게하십시오. ' – zaph

답변

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]; 
     } 
    } 
관련 문제