I am getting an unused variable warning for the trucksArray
array that I have declared in viewDidLoad. I don't understand why, because I am using it in another method within the viewController.m
.
I am super new to objective c, so I apologize in advance if this is a very straightforward question.
Here are the methods:
- (void)viewDidLoad
{
[super viewDidLoad];
TruckLocation *a1 = [[TruckLocation alloc] initWithName:@"test truck 1" address:@"41 Truck Avenue, Provo, Utah" coordinate:CLLocationCoordinate2DMake(40.300828, 111.663802)];
TruckLocation *a2 = [[TruckLocation alloc] initWithName:@"test truck 2" address:@"6 Truck street, Provo, Utah" coordinate:CLLocationCoordinate2DMake(40.300826, 111.663801)];
NSMutableArray* trucksArray =[NSMutableArray arrayWithObjects: a1, a2, nil];
}
and the method that I use the array:
- (void)plotTruckPositions:(NSData *)responseData {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSMutableArray *trucksArray = [root objectForKey:@"trucksArray"];
for (NSArray *row in trucksArray) {
NSNumber * latitude = [row objectAtIndex:1];
NSNumber * longitude = [row objectAtIndex:2];
NSString * truckDescription = [row objectAtIndex:2];
NSString * address = [row objectAtIndex:3];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
TruckLocation *annotation = [[TruckLocation alloc] initWithName:truckDescription address:address coordinate:coordinate] ;
[_mapView addAnnotation:annotation];
}
}
You're using two completely different arrays that both happen to be called trucksArray
.
In your viewDidLoad method, the array you're making isn't stored anywhere, so it goes out of scope and is released after the method returns. Did you mean to assign it to an instance variable?