在这里使用时,变量'lat'可能是未初始化的

时间:2015-03-20 01:12:42

标签: objective-c double

我正在尝试编译Apple示例:KMLViewer,但Xcode显示此错误:

  

变量'lat'在此处使用时可能未被初始化

代码是这样的:

// Convert a KML coordinate list string to a C array of CLLocationCoordinate2Ds
// KML coordinate lists are longitude,latitude[,altitude] tuples specified by whitespace.

static void strToCoords(NSString *str, CLLocationCoordinate2D **coordsOut, NSUInteger *coordsLenOut)
{
    NSUInteger read = 0, space = 10;
    CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * space);

    NSArray *tuples = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    for (NSString *tuple in tuples) {
        if (read == space) {
            space *= 2;
            coords = realloc(coords, sizeof(CLLocationCoordinate2D) * space);
        }

        double lat, lon;
        NSScanner *scanner = [[NSScanner alloc] initWithString:tuple];
        [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]];
        BOOL success = [scanner scanDouble:&lon];
        if (success) 
            success = [scanner scanDouble:&lat];
        if (success) {
            CLLocationCoordinate2D c = CLLocationCoordinate2DMake(lat, lon);
            if (CLLocationCoordinate2DIsValid(c))
                coords[read++] = c;
        }
    }

    *coordsOut = coords;
    *coordsLenOut = read;
}

Xcode“解决方案”将0.0添加到双纬度,lon(双纬度= 0.0,纬度),但不起作用。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您需要为变量设置默认值,因为只有在满足某些条件时才设置它们,但无论如何都要使用它们。这可能导致变量未设置但您尝试使用它们。 XCode建议您将变量默认为某个值。

即:

double lat = 0.0;
double lon = 0.0;