“无效的初始值设定项”编译时出错

发布于 2024-11-29 23:17:24 字数 1483 浏览 1 评论 0原文

我使用此代码

- (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;
}

并收到此错误:此行中的初始值设定项无效

CLLocationCooperative2D middlePosition = [self pointBetweenStartPoint:startPoint endPoint:endPoint 位置:位置];

怎么去除呢?

I use this code

- (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;
}

and getting this error: invalid initializer in this line

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

How remove it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-12-06 23:17:24

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

Is there a forward declaration of pointBetweenStartPoint:endPoint:position: visible (e.g. from an imported header file) before the block of code you quoted? If not, the compiler will assume the method returns id, which is not valid for initializing a CLLocationCoordinate2D struct, and give that error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文