konyv12
konyv12

Reputation: 776

How to access this array's elements (iOS objective-C)?

I have the following array defined:

NSArray *array = @[[NSNumber numberWithFloat:coordinate.latitude], [NSNumber numberWithFloat:coordinate.longitude]];
[tappedCoordinates addObject:array];

This is a multi-dimensional array (2 entries per each element). How may I retrieve the first and second entry per each element in the array?

Upvotes: 0

Views: 422

Answers (5)

vadian
vadian

Reputation: 285059

Why not even wrap the coordinate in NSValue? There is a dedicated method:

NSValue *value = [NSValue valueWithMKCoordinate:coordinate];
[tappedCoordinates addObject: value];

And to get the coordinate back:

NSValue *value = array[0];
CLLocationCoordinate2D coordinate = [value MKCoordinateValue];
CLLocationDegrees latitude = coordinate.latitude;
CLLocationDegrees longitude = coordinate.longitude;

Important note: To use this API you need to import MapKit.

Upvotes: 2

Sid Mhatre
Sid Mhatre

Reputation: 3417

To get 1st element of array try this :

 NSArray *array = [tappedCoordinates objectAtIndex:0]; 

To read elements of 1st array of array :

  NSLog(@"%@",[array objectAtIndex:0]);   
  NSLog(@"%@",[array objectAtIndex:0]); 

OR

  NSLog(@"%@",[[tappedCoordinates objectAtIndex:0] objectAtIndex:0]);   
  NSLog(@"%@",[[tappedCoordinates objectAtIndex:0] objectAtIndex:1]); 

Answer for your comment (Expected):

CLLocationCoordinate2D position;
for (int i = 0; i <= [tappedCoordinates count]-1; i++)
{
    position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
    position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = position;
    marker.map = _mapView;
}

Upvotes: 1

Lalit kumar
Lalit kumar

Reputation: 2197

Save latitude and longitude value in array as this

NSMutableArray *tappedCoordinates =[[NSMutableArray alloc]init];
NSDictionary *dict =  @{
  @"latitude" : [NSNumber numberWithFloat:coordinate.latitude],
  @"longitude" : [NSNumber numberWithFloat:coordinate.longitude],
 };

[tappedCoordinates addObject:dict];

NSLog(@"latitude is---%@",[[tappedCoordinates objectAtIndex:0]objectForKey:@"latitude"]);
NSLog(@"longitude is---%@",[[tappedCoordinates objectAtIndex:0]objectForKey:@"longitude"]);

Upvotes: 0

Avijit Nagare
Avijit Nagare

Reputation: 8782

Try This:

  NSLog(@"%@",[[tappedCoordinates objectAtIndex:0] objectAtIndex:0]);   
  NSLog(@"%@",[[tappedCoordinates objectAtIndex:0] objectAtIndex:1]); 
    //This will print NSNumber object from which you can get lat and lang

OR

 NSLog(@"%@",tappedCoordinates[0][0]);
  NSLog(@"%@",tappedCoordinates[0][1]);//Take care about conversion back to coordinate from NSNumber.

In case for for loop

for(int i=0;i< tappedCoordinates.count;i++){
    for(int j=0; j<[[tappedCoordinates objectAtIndex:i] count];j++){
       NSLog(@"%@",tappedCoordinates[i][j]);
    }
}

Upvotes: 1

Rikh
Rikh

Reputation: 4222

If what you are trying to achieve is a simple array of tapped CGPoints. You could try writing

[tappedCoordinates addObject [NSValue valueWithCGPoint:CGPointMake(coordinate.latitude, coordinate.longitude)]]

And then to get a value:

//or get any other position from the array :D
NSValue *value = [tappedCoordinates firstObject];

CGPoint coordinate = [value CGPointValue];

Upvotes: 0

Related Questions