2012-12-03 6 views
-1

현재 위치에서 대상 위치까지의 거리를 얻기 위해 for 루프를 사용하고 있습니다. 현재 위치에서 목적지까지의 모든 거리가 포함 된 배열을 원합니다.'for'루프에 의해 생성 된 배열에 요소를 추가하는 방법은 무엇입니까?

for (i = 0; i < [Arr_Lat count]; i++) 
{ 
    NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]]; 
    NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]]; 

    NSLog(@"First lat : %@",latst1); 
    NSLog(@"First ong : %@",longst1); 

    double Doblat1 = [latst1 doubleValue]; 
    double Doblong1 = [longst1 doubleValue]; 

    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1); 

    NSLog(@" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ; 

    CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long]; 

    CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1]; 

    NSLog(@" Currennt Location : %@", currLoc); 
    NSLog(@" Destination Location : %@" , destLoc1); 

    distance = [destLoc1 distanceFromLocation:currLoc]; 

    NSLog(@" Distance : ------- %0.3f", distance); 

    DistStr = [[NSString alloc]initWithFormat:@" %f",distance]; 

    [currLoc release]; 

    [destLoc1 release]; 
    [Arr_title retain]; 

    [tbl_nearplace reloadData]; 
} 

답변

0
NSMutableArray *Distance=[[NSMutableArray alloc]Init]; 

for (i = 0; i < [Arr_Lat count]; i++) 
{ 
    NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]]; 
    NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]]; 

    NSLog(@"First lat : %@",latst1); 
    NSLog(@"First ong : %@",longst1); 

    double Doblat1 = [latst1 doubleValue]; 
    double Doblong1 = [longst1 doubleValue]; 

    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1); 

    NSLog(@" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ; 

    CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long]; 

    CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1]; 

    NSLog(@" Currennt Location : %@", currLoc); 
    NSLog(@" Destination Location : %@" , destLoc1); 

    distance = [destLoc1 distanceFromLocation:currLoc]; 

    NSLog(@" Distance : ------- %0.3f", distance); 

    NSString *DistStr = [[NSString alloc]initWithFormat:@" %f",distance]; 

    [Distance addObject:DistStr]; 

    [DistStr release]; 
    [currLoc release]; 

    [destLoc1 release]; 
    [Arr_title retain]; 

    [tbl_nearplace reloadData]; 
} 
+0

예. 알겠습니다. 고마워 ........... –

+0

환영/................ – Venkat

1

거리를 저장하려면 NSMutableArray가 필요합니다.

  1. 클래스 범위에 NSMutableArray *distanceArray;을 선언하십시오.
  2. 초기화 : distanceArray = [[NSMutableArray alloc] init]; for 루프에서
  3. 후 다음 코드 쓰기 DistStr =[[NSString alloc]initWithFormat:@" %f",distance]; :

    [distanceArray addObject:DistStr]; 
    
+0

통해있는 NSMutableArray에게

NSMutableArray *theNewArrayWithOurStuff = [NSMutableArray array]; 

우리 루프를 reate? –

1

우리는 우리가 반복하여 다른 배열에 해당 항목을 추가 할 물건이 배열이 말을

NSArray *someArrayWithStuff = @[@"Hello", 
           @"Its", 
           @"Very", 
           @"Cold", 
           @"Outside", 
           @"Today"]; 

우리는 someArrayWithStuff의 내용을이 배열에 추가하여 c 여전히 문제가있는 someArrayWithStuff

for (int i = 0; i < someArrayWithStuff.count; i++) { 
    // Add object to the new array 
    [theNewArrayWithOurStuff addObject:[someArrayWithStuff objectAtIndex:i]]; 
} 
관련 문제