编译时“初始化程序无效”错误

时间:2011-08-16 11:19:48

标签: iphone xcode view cllocationmanager cllocation

我使用此代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    [self.window makeKeyAndVisible];

    // position is a number between 0 and 1, where 0 gives the start position, and 1 gives the end position

    CLLocationCoordinate2D startPoint = CLLocationCoordinate2DMake(37.9,-122.5);
    CLLocationCoordinate2D endPoint = CLLocationCoordinate2DMake(37.188022,-122.39979);

    for (int i = 1; i < 5; i++) {
        float position = i / 5.0;
    CLLocationCoordinate2D middlePosition =  [self pointBetweenStartPoint:startPoint endPoint:endPoint position:position];
    NSLog("%f, %f", middlePosition.latitude, middlePosition.longitude);
    }

    return YES;
}

- (CLLocationCoordinate2D)pointBetweenStartPoint:(CLLocationCoordinate2D)startPoint endPoint:(CLLocationCoordinate2D)endPoint position:(float)position {

    CLLocationDegrees latSpan = endPoint.latitude - startPoint.latitude;
    CLLocationDegrees longSpan = endPoint.longitude - startPoint.longitude;

    CLLocationCoordinate2D ret = CLLocationCoordinate2DMake(startPoint.latitude + latSpan*position,
startPoint.longitude + longSpan*position);
    return ret;
}

并收到此错误:此行中的初始化程序无效

  

CLLocationCoordinate2D middlePosition = [self pointBetweenStartPoint:startPoint endPoint:endPoint position:position];

如何删除它?

1 个答案:

答案 0 :(得分:2)

在您引用的代码块之前是否有pointBetweenStartPoint:endPoint:position:可见的前向声明(例如来自导入的头文件)?如果没有,编译器将假定该方法返回id,这对于初始化CLLocationCoordinate2D结构无效,并给出该错误。