Why is an array declared in viewDidLoad getting an unused variable warning when it is used in another method?

StackOverflow https://stackoverflow.com/questions/18626970

Question

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];
    }
}
Was it helpful?

Solution 2

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?

OTHER TIPS

In viewDidLoad you are creating an instance of NSMutableArray which gets deallocated after the viewDidLoad method ends. In the second method, you create an entirely different NSMutableArray. You should create an instance variable or a property to keep a reference to that NSMutableArray if you intent to create it somewhere and use it somewhere else.

@property (nonatomic) NSMutableArray *trucksArray;

If you declared the variables as global variables inside header (with curly brackets), you just need to assign them, no need for declaring them anew just assigning them (equal sign), i.e. leave out the "TruckLocation *" in front.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top