当我在 Xcode 中分析我的项目时,我得到了一些奇怪的错误。所有这些代码都是单个方法的一部分,该方法创建可用于制作MKAnnotations
. 抱歉,如果这是代码泛滥 - 我已尽力注释掉不相关的部分。除了片段之外,我还包括了整个方法,只是为了某种角度。谢谢!
- (void)addLines {
/*
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Create region and span variables
MKCoordinateSpan span;
MKCoordinateRegion region;
NSArray* arrayOuter = [defaults objectForKey:@"mapSaveDataKey"];
NSData* data = [arrayOuter objectAtIndex:[[defaults objectForKey:@"currentMap"] intValue]];
NSArray* arrayOne = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (arrayOne.count == 0)
[self returns:nil];
*/
NSArray* overlayLat = [arrayOne objectAtIndex:1];
double lats[overlayLat.count];
NSArray* overlayLong = [arrayOne objectAtIndex:2];
double longs[overlayLong.count];
NSArray* annotationLat = [arrayOne objectAtIndex:8];
double annotationLats[annotationLat.count];
NSArray* annotationLong = [arrayOne objectAtIndex:9];
double annotationsLongs[annotationLong.count];
/*
CLLocationCoordinate2D startLocation = CLLocationCoordinate2DMake([[overlayLat objectAtIndex:0] doubleValue], [[overlayLong objectAtIndex:0] doubleValue]);
CLLocationCoordinate2D finishLocation = CLLocationCoordinate2DMake([[overlayLat objectAtIndex:[overlayLat count] - 1] doubleValue], [[overlayLong objectAtIndex:[overlayLong count] - 1] doubleValue]);
NSString* string1 = [[[NSString alloc] initWithFormat:@"Lat: %.3f, Long: %.3f", [[overlayLat objectAtIndex:0] doubleValue], [[overlayLong objectAtIndex:0] doubleValue]] autorelease];
NSString* string2 = [[[NSString alloc] initWithFormat:@"Lat: %.3f, Long: %.3f", [[overlayLat objectAtIndex:([overlayLat count] - 1)] doubleValue], [[overlayLong objectAtIndex:([overlayLong count] - 1)] doubleValue]] autorelease];
LocationAnnotation* startAnnotation = [[LocationAnnotation alloc] initWithCoordinate:startLocation title:@"Start" subtitle:string1];
LocationAnnotation* finishAnnotation = [[LocationAnnotation alloc] initWithCoordinate:finishLocation title:@"Finish" subtitle:string2];
*/
for (int iii = 0; iii < overlayLat.count; iii++) {
NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
lats[iii] = [a doubleValue];
}
for (int iii = 0; iii < overlayLong.count; iii++) {
NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
longs[iii] = [a doubleValue];
}
for (int iii = 0; iii < annotationLong.count; iii++) {
NSNumber* a = (NSNumber*)[annotationLong objectAtIndex:iii];
annotationsLongs[iii] = [a doubleValue];
}
for (int iii = 0; iii < annotationLat.count; iii++) {
NSNumber* a = (NSNumber*)[annotationLat objectAtIndex:iii];
annotationLats[iii] = [a doubleValue];
}
int sizeLats = 0;
for (int iii = 0; iii < overlayLat.count; iii++)
if (lats[iii] != 0)
sizeLats++;
CLLocationCoordinate2D coords[sizeLats];
for (int iii = 0; iii < sizeLats; iii++) {
if (lats[iii] != 0 && longs[iii] != 0) {
coords[iii].latitude = lats[iii];
coords[iii].longitude = longs[iii];
} else {
coords[iii].latitude = coords[iii - 1].latitude;
coords[iii].longitude = coords[iii - 1].longitude;
}
}
/*
// Give variables value
span = MKCoordinateSpanMake(.05, .05);
region = MKCoordinateRegionMake(coords[0], span);
MKPolyline* line = [MKPolyline polylineWithCoordinates:coords count:sizeLats];
[mapView addOverlay:line];
[mapView addAnnotations:[NSArray arrayWithObjects:startAnnotation, finishAnnotation, nil]];
[mapView selectAnnotation:startAnnotation animated:YES];
*/
for (int iii = 0; iii < annotationLong.count; iii++) {
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(annotationLats[iii], annotationsLongs[iii]);
/*
NSString* subtitle = [[[NSString alloc] initWithFormat:@"Lat: %.3f, Long: %.3f", coord.latitude, coord.longitude] autorelease];
NSString* title = [[[NSString alloc] initWithFormat:@"Stop %i", (iii + 1)] autorelease];
LocationAnnotation* a = [[LocationAnnotation alloc] initWithCoordinate:coord title:title subtitle:subtitle];
CLLocation* c = [[CLLocation alloc] initWithCoordinate:coord altitude:0 horizontalAccuracy:0 verticalAccuracy:0 course:0 speed:0 timestamp:0];
CLLocation* d = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(finishAnnotation.coordinate.latitude, finishAnnotation.coordinate.longitude) altitude:0 horizontalAccuracy:0 verticalAccuracy:0 course:0 speed:0 timestamp:0];
if ([c distanceFromLocation:d] > 20)
[mapView addAnnotation:a];
[c release];
[d release];
[a release];
*/
}
/*
[mapView setRegion:region animated:YES];
[startAnnotation release];
[finishAnnotation release];
*/
}
第一的:
int sizeLats = 0;
for(int iii = 0; iii < overlayLat.count; iii++)
if(lats[iii] != 0) //"The left operand of '!=' is a garbage value"
sizeLats++;
Xcode 怎么敢指责我这种恶作剧?
第二:
CLLocationCoordinate2D coords[sizeLats]; //"Declared variable-length array (VLA) has zero size"
第三:
for (int iii = 0; iii < sizeLats; iii++) {
if (lats[iii] != 0 && longs[iii] != 0) { // "The left operand of '!=' is a garbage value"
coords[iii].latitude = lats[iii];
coords[iii].longitude = longs[iii];
} else {
coords[iii].latitude = coords[iii - 1].latitude; // "Assigned value is garbage or undefined"
coords[iii].longitude = coords[iii - 1].longitude;
}
}
第四:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(annotationLats[iii], annotationsLongs[iii]); // "Function call argument is an uninitialized value"
除了,我刚刚初始化了那个数组?我知道如果annotationLats
并且annotationLongs
用零长度初始化它可能会未初始化。
这些警告是否重要?如果我的程序在没有这些警告的情况下按预期运行,我可以安全地忽略它们吗?我只是觉得忽略 Xcode 不是一个好主意——它相当聪明。