2011-12-16 7 views
0

주소 및 연락처 세부 정보가 포함 된 여러 사전 배열이 있습니다. 사전의 각 배열을 순환하여 각 주소의 위치를 ​​찾아 해당 위도와 경도를 저장하고 싶습니다. 어떤 사전 배열이라 할지라도 두 개의 추가 키 "경도"와 "위도"를 각 값으로 추가 할 수 있기를 원합니다.iPhone : 기존 사전 배열에 추가 개체와 키를 추가하는 방법은 무엇입니까?

현재 새 사전을 만들고 각 키와 값 [새 경도와 위도를 포함하여]를 읽고 완전히 새로운 배열을 작성합니다. 너무 복잡하게 보입니다. 더 좋은 방법이 있습니까?

 //copy into new array 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"Category"]  forKey:@"Category"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"Name"]   forKey:@"Name"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"Address"]  forKey:@"Address"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"PhoneNumber"] forKey:@"PhoneNumber"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"ContactTitle"] forKey:@"ContactTitle"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"ContactName"] forKey:@"ContactName"]; 

//APPENDING LOCATION KEYS AND DATA 

    [dict setObject:[[array objectAtIndex:x] objectForKey:@"Longitude"] forKey:@"Longitude"]; 
    [dict setObject:[[array objectAtIndex:x] objectForKey:@"Latitude"]  forKey:@"Latitude"]; 

    [masterArrayWithDistance addObject:[dict copy]]; 
+0

더 좋은 아이디어는 모든 키에 대해 배열에 한 번 액세스하는 대신 임시 myObject = [array objectAtIndex : x]를 사용하는 것입니다. – mga

+0

또는 임시 NSMutableDictionary * myDict = [NSDictionary dictionaryWithDictionary : [배열 objectAtIndex : x]];'새 객체를 추가하고 마스터에 삽입 – mga

답변

1

배열/사전의 mutableCopy를 만들고 싶습니다.

NSMutableArray *copyArray = [originalArray mutableCopy]; 
NSMutableDictionary *copyDic = [[array objectAtIndex:x] mutableCopy]; 
[copyDic setObject:[[array objectAtIndex:x] objectForKey:@"Longitude"] forKey:@"Longitude"]; 
[copyDic setObject:[[array objectAtIndex:x] objectForKey:@"Latitude"]  forKey:@"Latitude"]; 

그런 다음 이전 사전/배열을 새 것으로 바꿀 수 있습니다.

+0

위대한. 고마워요 마이클 – Jeremy

1

한 가지 방법 :

NSMutableDictionary* d = [NSMutableDictionary dictionaryWithDictionary: dict]; 
[d setObject: newLongitude forKey: @"Longitude"]; 
[d setObject: newLatitude forKey: @"Latitude"]; 
[masterArrayWithDistance addObject: d]; 

또한, 당신이 처음에 사전을 복사하는 데 필요한 이유는 확실하지 않다. addObject:은 참조를 유지합니다.

+0

우수. 감사 데이빗 – Jeremy

관련 문제