2012-03-29 2 views
0

배열의 각 색인에서 처음 세 키 값을 제거해야합니다. 내가 한 일은키 값을 제거한 후 배열을 사전으로 변환하십시오.

for (int i=0; i<[normalscoringarray count]; i++)   
    { 

     NSDictionary *dictionary = [normalscoringarray objectAtIndex:i]; 

     NSMutableDictionary *mutableDictionary = [dictionary mutableCopy]; 

     [mutableDictionary removeObjectForKey:@"cn"]; 
     [mutableDictionary removeObjectForKey:@"gdate"]; 
     [mutableDictionary removeObjectForKey:@"gid"]; 

     NSLog(@"mutableDictionary.....%@",mutableDictionary); 

    NSMutableArray *remainingArray = [[NSMutableArray alloc]init]; 

     for (id item in mutableDictionary) { 
      [remainingArray setArray:[mutableDictionary objectForKey:item]]; 
     } 
     } 

이며, 내가 원하는 순서대로 remainingArray를 제공하지 않습니다. 더 이상 모든 키를 제거합니다.

normalscoringarray....(
    { 
    cn = "Ranjit Garh"; 
    gdate = "2012-03-25"; 
    gid = 1; 
    id = 1; 
    p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60"; 
    p1nh = "BP||14||1"; 
    p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61"; 
    p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48"; 
    p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67"; 
    p2nh = "DG||18||2"; 
    p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67"; 
    p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41"; 
    p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72"; 
    p3nh = "NM||24||3"; 
    p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73"; 
    p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36"; 
    p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69"; 
    p4nh = "KS||20||4"; 
    p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70"; 
    p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39"; 
}, 
    { 
    cn = "Eden Garden"; 
    gdate = "2012-03-26"; 
    gid = 2; 
    id = 2; 
    p1nets = "3,2,5,3,1,2,2,4,4,3,4,3,5,2,5,3,3,5||26||33||59"; 
    p1nh = "NM||24||3"; 
    p1score = "5,4,6,5,3,4,3,6,5,4,5,4,6,3,6,4,4,6||41||42||83||59"; 
    p1spoints = "3,4,2,3,4,4,3,3,2,3,2,3,1,3,2,2,3,2||28||21||49"; 
    p2nets = "2,4,5,3,2,4,3,4,4,4,3,3,5,2,5,3,4,4||31||33||64"; 
    p2nh = "KS||20||4"; 
    p2score = "4,5,6,4,3,6,4,5,5,5,4,4,6,3,6,4,5,5||42||42||84||64"; 
    p2spoints = "4,2,2,3,3,2,2,3,2,2,3,3,1,3,2,2,2,3||23||21||44"; 
    p3nets = "3,4,5,5,3,5,4,5,5,5,4,5,5,4,5,3,5,6||39||42||81"; 
    p3nh = "AK||18||5"; 
    p3score = "4,5,6,6,4,6,5,6,6,6,5,6,6,5,6,4,6,7||48||51||99||81"; 
    p3spoints = "3,2,2,1,2,1,1,2,1,1,2,1,1,1,2,2,1,1||15||12||27"; 
    p4nets = "2,4,5,4,3,3,3,5,5,4,3,5,4,3,5,4,5,6||34||39||73"; 
    p4nh = "KR||20||6"; 
    p4score = "4,5,6,5,4,5,4,6,6,5,4,6,5,4,6,5,6,7||45||48||93||73"; 
    p4spoints = "4,2,2,2,2,3,2,2,1,2,3,1,2,2,2,1,1,1||20||15||35"; 
}, 
) 

방법은 내가

normalscoringarray....(
    id = 1, 
    p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60", 
    p1nh = "BP||14||1", 
    p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61", 
    p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48", 
    p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67", 
    p2nh = "DG||18||2", 
    p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67", 
    p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41", 
    p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72", 
    p3nh = "NM||24||3", 
    p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73", 
    p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36", 
    p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69"; 
    p4nh = "KS||20||4"; 
    p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70", 
    p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39", 
) 

답변

0
NSMutableArray *remainingArray = [[NSMutableArray alloc]init]; 

for (int i=0; i<[normalscoringarray count]; i++)   
{ 
    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:[normalscoringarray objectAtIndex:i]]; //each time alloc and init with contents of normalscoringarray 

    //Remove values for keys 
    [mutableDictionary removeObjectForKey:@"cn"]; 
    [mutableDictionary removeObjectForKey:@"gdate"]; 
    [mutableDictionary removeObjectForKey:@"gid"]; 

    [remainingArray addObject:mutableDictionary]; //add mutableDictionary to remainingArray 

    [mutableDictionary release]; //free mutableDictionary 
} 

//finally print contents of remainingArray 
NSLog(@"normalscoringarray:\n%@",[remainingArray description]); 

희망이 당신이 도움이 될 것입니다 싶다!

+0

더 이상 필요하지 않을 때 remainingArray를 릴리스하는 것을 잊지 마십시오. – Hemang

+1

Ketan, 결과에서 원하는 방식에 대해 질문을했는데 왜 형식이 필요한지 이해할 수 없습니까? 해당 키를 통해 모든 값에 직접 액세스 할 수 있기 때문입니다. – Hemang

+0

사실 나는 배열의 모든 키가 필요한 이유를 확인하는 목적으로 'for 루프'를 모든 키에 적용하려고하지만 ... 나는 그것을 얻지 못하고있다. – Ketan

0
for (int i=0; i<[normalscoringarray count]; i++)   
{  
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:[normalscoringarray objectAtIndex:i]]; 


    [dictionary removeObjectForKey:@"cn"]; 
    [dictionary removeObjectForKey:@"gdate"]; 
    [dictionary removeObjectForKey:@"gid"]; 

     [normalscoringarray replaceObjectAtIndex:i withObject:dictionary]; 
} 
관련 문제