首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能为不同的字典设置不同的值

问不能为不同的字典设置不同的值
EN

Stack Overflow用户
提问于 2013-05-04 13:09:03
回答 1查看 93关注 0票数 0

我通过NSDictionaries循环计算从用户位置到注释的距离,如下所示:

代码语言:javascript
复制
CLLocation *pinLocation = [[CLLocation alloc]
                                       initWithLatitude:realLatitude
                                       longitude:realLongitude];

CLLocation *userLocation = [[CLLocation alloc]
                                        initWithLatitude:mapView.userLocation.coordinate.latitude
                                        longitude:mapView.userLocation.coordinate.longitude];

CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

结果:

代码语言:javascript
复制
NSLog(@"Distance: %4.0f m.", distance);

2013-05-04 15:58:53.301 testApp[3194:907] Distance: 65758 m.
2013-05-04 15:58:53.304 testApp[3194:907] Distance: 91454 m.
2013-05-04 15:58:53.308 testApp[3194:907] Distance: 248726 m.
2013-05-04 15:58:53.310 testApp[3194:907] Distance: 297228 m.
2013-05-04 15:58:53.313 testApp[3194:907] Distance: 163058 m.

然后,我尝试在数组中为字典添加距离。

代码语言:javascript
复制
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];

[ann setValue:dist forKey:@"Distance"];

但它只增加了最后一段距离(距离: 163058米)。所有的字典。

如何为不同的字典设置不同的值?

编辑:我的循环

代码语言:javascript
复制
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"])
    {

        ann = [dict objectForKey:@"Blue"];

        [resultArray addObject:@"Blue"];


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


            NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

            double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
            double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

            MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
            CLLocationCoordinate2D theCoordinate;
            theCoordinate.latitude = realLatitude;
            theCoordinate.longitude = realLongitude;

            myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
            myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
            myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
            myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

            [mapView addAnnotation:myAnnotation];
            [annotations addObject:myAnnotation];



            // Calculating distance
            CLLocation *pinLocation = [[CLLocation alloc]
                                       initWithLatitude:realLatitude
                                       longitude:realLongitude];

            CLLocation *userLocation = [[CLLocation alloc]
                                        initWithLatitude:mapView.userLocation.coordinate.latitude
                                        longitude:mapView.userLocation.coordinate.longitude];

            CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

            // NSLog(@"Distance: %4.0f m.", distance);


            NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
            [ann setValue:dist forKey:@"Distance"];


        }



    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-04 13:23:04

您可以添加某些Array中的所有值,然后可以使用它保存在不同的字典中。

在这条线之后:

代码语言:javascript
复制
// NSLog(@"Distance: %4.0f m.", distance);

加:

代码语言:javascript
复制
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:@"Distance"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16374643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档