“无效的初始值设定项”编译时出错
我使用此代码
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您引用的代码块之前是否存在可见的
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 returnsid
, which is not valid for initializing aCLLocationCoordinate2D
struct, and give that error.