gpt4 book ai didi

ios - 无法识别的选择器发送到实例错误 - 我的错误在哪里?

转载 作者:行者123 更新时间:2023-11-29 00:26:11 24 4
gpt4 key购买 nike

我不确定我的代码有什么问题。以下是我可能有问题的 3 个函数。这是一款适用于 iOS 的 Google map 应用。

错误消息:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI floatValue]:无法识别的选择器发送到实例 0x60000042e680”

     - (void)drawPolygon{
GMSMutablePath *rect = [GMSMutablePath path];
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
event.latitude = [[tappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[tappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
// first tapped point to connect with last point in order to close the polygon.
event.latitude = [[tappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[tappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
...
}

- (void)addMarker{
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
...
}

最佳答案

您只是没有深入到数组中以获取实际的 long/lat 值:

for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
event.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
event.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
[rect addCoordinate:event];
}

event.latitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:0] floatValue];
event.longitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:1] floatValue];

编辑:只是为了澄清 - [tappedCoordinates objectAtIndex:0] 是一个 NSArray。当您在其上调用 floatValue 时,NSArray 不知道该怎么做,因为它不是数值。此外,您的方法的替代方法是拥有一个 CGPoint 对象数组(比嵌套数组更好的实现):

NSMutableArray *coords = [NSMutableArray new];
CGPoint coord = CGPointMake(1.5f, 2.5f);
[coords addObject:[NSValue valueWithCGPoint: coord]];

CGPoint storedCoord = [[coords objectAtIndex:0] CGPointValue];
NSLog (@"Lat: %f, long: %f", storedCoord.x, storedCoord.y);

您甚至可以更进一步,创建一个子类 CGPoint(名为 Coordinate),它具有纬度和经度属性,可以分别返回 x 和 y(主要是为了可读性)。

关于ios - 无法识别的选择器发送到实例错误 - 我的错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42962307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com