为啥不将值添加到 NSMutableArray?

Posted

技术标签:

【中文标题】为啥不将值添加到 NSMutableArray?【英文标题】:Why value is not added to a NSMutableArray?为什么不将值添加到 NSMutableArray? 【发布时间】:2014-09-29 13:25:17 【问题描述】:

我正在尝试使用纬度和经度来初始化 CLLocation。然后将其添加到NSMutableArray。我未来的计划是在地图上显示这些值。但是,我正在努力使用以下代码,并且我确信我在某个地方犯了一个愚蠢的错误,但由于我是新手,所以无法弄清楚。如果有人能发现它,我将不胜感激:

这是输出:

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

如您所见,它在循环内执行了四次,但不会添加任何内容:

    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;

这是变量的定义:

@implementation InfoViewController

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

double lastlat;
double lastlong;

....

更新

我已经添加了这个并且它正在工作:

- (void)viewDidLoad 

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

【问题讨论】:

你初始化你的可变数组了吗?在 viewdidload trackPointArray = [NSMutableArray new]; 的某处添加此代码 可能您的NSMutableArray 未初始化。 你有没有初始化trackPointArray 指向一个实际的数组对象? 【参考方案1】:

正如 cmets 中的人们已经说过的那样,在 InfoViewController 中覆盖 -viewDidLoad,如下所示:

- (void)viewDidLoad

    [super viewDidLoad];

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

【讨论】:

以上是关于为啥不将值添加到 NSMutableArray?的主要内容,如果未能解决你的问题,请参考以下文章

为啥不将其添加到数据库中?

为啥不将 FirebaseDatabase 引用中的所有数据都添加到 ArrayList<String> 中?

如何将值插入 NSMutableArray? [关闭]

在 python2.7 中使用 pool.apply_async 不将值插入 MySQL 表

为啥 NSMutableArray 元素没有被释放?

为啥 StandardScaler 不将元数据附加到输出列?