简体   繁体   中英

Why value is not added to a NSMutableArray?

I am trying to initial CLLocation with Latitude and Longitude. Then add it to a NSMutableArray . My future plan is to show these values on the map. However, I am struggling with following code and I am sure I am doing somewhere a silly mistake but can't figure it out as I am newbie. I appreciate if anyone can spot it:

Here is output:

2014-09-29 23:16:02.261 JourneyTracker[8149:343967] lat= 37.33446146  long= -122.04380955   
2014-09-29 23:16:02.261 JourneyTracker[8149:343967] before  numberOfSteps== 0
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] After   numberOfSteps== 0
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] lat= 37.33445363  long= -122.04302852   
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] before  numberOfSteps== 0
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] After   numberOfSteps== 0
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] lat= 37.33445283  long= -122.04113765   
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] before  numberOfSteps== 0
2014-09-29 23:16:02.945 JourneyTracker[8149:343967] After   numberOfSteps== 0
2014-09-29 23:16:02.945 JourneyTracker[8149:343967] lat= 37.33445945  long= -122.04342255   
2014-09-29 23:16:02.946 JourneyTracker[8149:343967] before  numberOfSteps== 0
2014-09-29 23:16:03.053 JourneyTracker[8149:343967] After   numberOfSteps== 0
2014-09-29 23:16:03.054 JourneyTracker[8149:343967] 111numberOfSteps== 0
2014-09-29 23:16:09.390 JourneyTracker[8149:343967] 2222numberOfSteps== 0
2014-09-29 23:16:10.238 JourneyTracker[8149:343967] 3333numberOfSteps== 0

As you can see it goes four times inside the loop but nothing will be added:

    for(Coordinates *cord in coordFromDB)
    {
        NSLog(@"lat= %@  long= %@   ",cord.lat,cord.longt);

        CLLocationDegrees Latitude = [cord.lat doubleValue];
        CLLocationDegrees Longitude = [cord.longt doubleValue];

        lastlat = [cord.lat doubleValue];
        lastlong = [cord.longt doubleValue];

        CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);

        //Zoom map to show current location
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 2000, 2000);
        MKCoordinateRegion adjustRegion = [self.mapView regionThatFits:viewRegion];
        [self.mapView setRegion:adjustRegion animated:YES];

        CLLocation *currLocation = [[CLLocation alloc] initWithLatitude:lastlong longitude:lastlong];

        NSLog(@"before  numberOfSteps== %d",trackPointArray.count);
        //Store latest location in stored track array

        [trackPointArray addObject:currLocation];

        NSLog(@"After   numberOfSteps== %d",[trackPointArray count]);
    }


    NSLog(@"111numberOfSteps== %d",trackPointArray.count);


    NSInteger numberOfSteps =trackPointArray.count;


    NSLog(@"2222numberOfSteps== %d",numberOfSteps);


    CLLocationCoordinate2D coordinates[numberOfSteps];

    for(NSInteger index=0;index<numberOfSteps;index++)
    {
        CLLocation *location = [trackPointArray objectAtIndex:index];
        CLLocationCoordinate2D coordinate2 = location.coordinate;
        coordinates[index] = coordinate2;
    }


    routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.mapView addOverlay:routeLine];
    NSLog(@"3333numberOfSteps== %d",numberOfSteps);

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if (![overlay isKindOfClass:[MKPolygon class]]) {
        return nil;
    }
    MKPolygon *polygon = (MKPolygon *)overlay;
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];
    renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4];
    return renderer;
}

Here is definition of variables:

@implementation InfoViewController

NSMutableArray *trackPointArray;
MKMapRect routeRect;
MKPolylineView* routeLineView;
MKPolyline* routeLine;

double lastlat;
double lastlong;

....

Update

I have added this and it is working:

- (void)viewDidLoad {

    [super viewDidLoad];
     trackPointArray = [NSMutableArray new];

As people in comments already said, override -viewDidLoad in InfoViewController like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.trackPointArray = [@[] mutableCopy];
    // Other initialization code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM