Get Array With Array but it's goes array in array
-
21-12-2019 - |
Question
I have data on database like this.
["{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}"]
Then i try to query data from database by following code.
- (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);
}
}];
}
I get the value of [objects valueForKey:@"Path"]
( ( "{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}" ) )
But i want it to
( "{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}" )
What should i do?
Solution
It looks as if objects
is an array of one element, which is an object with the "Path"
property. In that case you should replace
NSArray *pointsArray = [objects valueForKey:@"Path"];
by
NSArray *pointsArray = [objects[0] valueForKey:@"Path"];
OTHER TIPS
In your example you have the objectId. If that is the case, you use getObjectWithId. This will return only the object you want.
PFQuery *query = [PFQuery queryWithClassName:@"Session"];
[query getObjectWithId:@"hr46gjh45"];
Your example returns an array of objects; in this case the array has only one object (but it is still an array). In that case you must first retrieve the object from the array.