【问题标题】:How can i contain all string to array?我怎样才能将所有字符串包含到数组中?
【发布时间】:2014-05-27 00:29:54
【问题描述】:

我尝试通过 Geopoint 将 GeoPoint 转换为 Array 到 String 到 Array。我想使用这个数组在地图套件中绘制折线。 现在我可以转换为字符串但不能包含数组。 我如何将所有字符串包含到数组中 有没有其他方法可以使用 GeoPoint 在地图套件中绘制折线?

- (void)updateLocations {
    CGFloat kilometers = self.radius/1000.0f;

    PFQuery *query = [PFQuery queryWithClassName:@"Location"];
    [query setLimit:1000];
    [query whereKey:@"location"
       nearGeoPoint:[PFGeoPoint geoPointWithLatitude:self.location.coordinate.latitude
                                           longitude:self.location.coordinate.longitude]
   withinKilometers:kilometers];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            static NSNumberFormatter *numberFormatter = nil;
            if (numberFormatter == nil) {
                numberFormatter = [[NSNumberFormatter alloc] init];
                numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
                numberFormatter.maximumFractionDigits = 6;
            }
            //NSArray *path = [objects valueForKey:@"location"]; //dictionary to array!!!
            //NSLog(@"%@ %@",objects,path);
            NSMutableArray *muarray;
            for (PFObject *object in objects) {

                PFGeoPoint *geoPoint = object[@"location"];
                NSString *string = [NSString stringWithFormat:@"{%@, %@}",
                                    [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.latitude]],
                                    [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.longitude]]];
                NSLog(@"%@",string);
                [muarray addObject:string]; //I'm try this but it's fail.
                NSLog(@"%@",muarray);


            }
        }
    }];
}

【问题讨论】:

  • 它是怎么失败的?什么错误?
  • 获取日志muarray值“(null)”
  • 是的,我回答了这个问题——我在问完这个问题后就注意到了这个问题 :)

标签: ios arrays mapkit polyline geopoints


【解决方案1】:

您需要创建一个数组来添加字符串。如果您确实创建了一个NSMutableArray 并将其分配给muarray,那么您基本上是将字符串添加到nil,它什么都不做。

        NSMutableArray *muarray = [NSMutableArray array];
        for (PFObject *object in objects) {

            PFGeoPoint *geoPoint = object[@"location"];
            NSString *string = [NSString stringWithFormat:@"{%@, %@}",
                                [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.latitude]],
                                [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.longitude]]];
            NSLog(@"%@",string);
            [muarray addObject:string]; //I'm try this but it's fail.
            NSLog(@"%@",muarray);


        }

【讨论】:

  • 我明白了!谢谢你,里奇。
猜你喜欢
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
相关资源
最近更新 更多