1

我有这样的数据库数据。

["{37.331622, -122.030337}","{37.331593, -122.03051}","{37.331554, -122.030681}","{37.331383, -122.030757}","{37.33108, -122.030772}","{37.330798, -122.030729}","{37.330636, -122.030636}"]

然后我尝试通过以下代码从数据库中查询数据。

- (void)updateLocations {
    CGFloat kilometers = self.radius/1000.0f;
    //PFUser *user = [PFUser currentUser];
    PFQuery *query = [PFQuery queryWithClassName:@"Session"];
    [query whereKey:@"objectId" equalTo:@"t2udAri048"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"objects %@",objects);
            NSLog(@"path %@",[objects valueForKey:@"Path"]);
            NSArray *pointsArray = [objects valueForKey:@"Path"];;
            NSInteger pointsCount = pointsArray.count;
            CLLocationCoordinate2D pointsToUse[pointsCount];

            for(int i = 0; i < pointsCount; i++) {
                CGPoint p = CGPointFromString(pointsArray[i]);
                pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
            }

            MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
            [self.mapView addOverlay:myPolyline];
            //NSLog(@"Drawed %@",pointsArray);
        }
    }];
}

我得到 [objects valueForKey:@"Path"] 的值

( ( "{37.331622, -122.030337}", "{37.331593, -122.03051}", "{37.331554, -122.030681}", "{37.331383, -122.030757}", "{37.33108, -122.0.330779), "{37.33108, -122.0.30779 , -122.030729}”, “{37.330636, -122.030636}” ) )

但我想要它

( "{37.331622, -122.030337}", "{37.331593, -122.03051}", "{37.331554, -122.030681}", "{37.331383, -122.030757}", "{37.33108, -122.0330779}, "{7. -122.030729}", "{37.330636, -122.030636}")

我应该怎么办?

4

2 回答 2

1

看起来好像objects是一个元素的数组,它是一个具有“路径”属性的对象。在这种情况下,您应该更换

NSArray *pointsArray = [objects valueForKey:@"Path"];

经过

NSArray *pointsArray = [objects[0] valueForKey:@"Path"];
于 2014-04-13T14:41:18.110 回答
0

在您的示例中,您有 objectId。如果是这种情况,则使用 getObjectWithId。这将只返回您想要的对象。

PFQuery *query = [PFQuery queryWithClassName:@"Session"];

[查询getObjectWithId:@"hr46gjh45"];

您的示例返回一个对象数组;在这种情况下,数组只有一个对象(但它仍然是一个数组)。在这种情况下,您必须首先从数组中检索对象。

于 2014-04-13T15:09:07.847 回答