如何将元素添加到由'for'循环创建的数组中?

时间:2012-12-03 11:33:40

标签: iphone

我正在使用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];
} 

3 个答案:

答案 0 :(得分:1)

如果你想存储距离,你需要一个NSMutableArray。

  1. 在班级范围内声明NSMutableArray *distanceArray;
  2. 初始化它:distanceArray = [[NSMutableArray alloc] init];
  3. DistStr =[[NSString alloc]initWithFormat:@" %f",distance];之后的for循环中 写下面的代码:

    [distanceArray addObject:DistStr];
    

答案 1 :(得分:1)

假设我们有这个数组包含我们想要迭代的东西并将这些项添加到另一个数组

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

假设我们希望将someArrayWithStuff的内容添加到另一个数组中,以便我们创建一个NSMutableArray

NSMutableArray *theNewArrayWithOurStuff = [NSMutableArray array];

我们遍历someArrayWithStuff

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

答案 2 :(得分: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];
}