GMSPolygon数组iOS崩溃

时间:2016-04-13 18:51:27

标签: ios objective-c arrays google-maps polygon

我对GMSPolygon对象有一个非常奇怪的问题。 我的代码突然崩溃,错误"无法识别的选择器发送到实例0x ...."

(是的,它整天都在工作,突然间它开始崩溃)

我正在使用多边形数组(以跟踪它们并动态更新它们)并在我的-viewDidLoad中按如下方式初始化它们:

GMSPolygon *myPoly[50];

-

GMSMutablePath *path = [[GMSMutablePath alloc] init];
// set some fake coordinates, initializing a Polygon with an empty path seems to crash as well...

[path addCoordinate:CLLocationCoordinate2DMake(1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(-1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(0,1)];

for (int i=0;i<50;i++) {
    myPoly[i] = [GMSPolygon  polygonWithPath:path];
    myPoly[i].map = nil;
}

稍后在我的程序中,我尝试以相同的方式再次访问该对象,首先将其设置为nil,以便将其从地图中删除,然后更新并在必要时再次显示

for (int i=0;i<50;i++) {
    myPoly[i] = [GMSPolygon  polygonWithPath:path];
    myPoly[i].map = nil;  <--------- CRASH 


    // Do other stuf here, update the Polygon data and if needed
    // display again as follows:
    myPoly[i].map = mapView_;

}

但似乎崩溃了...... 如果我将GMSPolygon放在NSMutable数组中,也会发生同样的事情。初始化数组很好,但是将GMSPolygon从数组中取出并设置.map属性会导致同样的崩溃。

更新:

它似乎是由对象Memory位置引起的。如果它工作正常,内存位置如下:

[0] GMSPolygon * 0x1558ed750 0x00000001558ed750&lt; --- viewDidLoad for-loop

[0] GMSPolygon * 0x1558ed750 0x00000001558ed750&lt; ---其他函数for-loop

  • 崩溃时

[0] GMSPolygon * 0x12e7cbf30 0x000000012e7cbf30&lt; --- viewDidLoad for-loop

[0] GMSPolygon * 0x129d36630 0x0000000129d36630&lt; ---其他函数for-loop

该对象仅在viewDidLoad中初始化一次,其他地方都没有! 它显然解释了如果对象内存位置不同的崩溃......但是这里发生了什么?

任何人都知道为什么?

更新2,现在收到崩溃日志:

2016-04-14 15:16:49.618 myApp[1130:240689] -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.627 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 354] CRASH: -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.688 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 355] Stack Trace: (
0   CoreFoundation                      0x0000000182ebee50 <redacted> + 148
1   libobjc.A.dylib                     0x0000000182523f80 objc_exception_throw + 56
2   CoreFoundation                      0x0000000182ec5ccc <redacted> + 0
3   CoreFoundation                      0x0000000182ec2c74 <redacted> + 872
4   CoreFoundation                      0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
5   myApp                               0x00000001001115a0 -[mapViewController plotPoly] + 2028
6   myApp                               0x000000010012c944 -[mapViewController mapView:didChangeCameraPosition:] + 556
7   CoreFoundation                      0x0000000182ec4ae0 <redacted> + 144
8   CoreFoundation                      0x0000000182dbc548 <redacted> + 284
9   CoreFoundation                      0x0000000182dc0e70 <redacted> + 60
10  myApp                              0x0000000100203370 -[GMSDelegateForward forwardInvocation:] + 108
11  CoreFoundation                      0x0000000182ec2aa4 <redacted> + 408
12  CoreFoundation                      0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
13  myApp                               0x0000000100188fec -[GMSMapView updateWithCamera:] + 176
14  Foundation                          0x0000000183893ffc <redacted> + 340
15  CoreFoundation                      0x0000000182e75124 <redacted> + 24
16  CoreFoundation                      0x0000000182e74bb8 <redacted> + 540
17  CoreFoundation                      0x0000000182e728b8 <redacted> + 724
18  CoreFoundation                      0x0000000182d9cd10 CFRunLoopRunSpecific + 384
19  GraphicsServices                    0x0000000184684088 GSEventRunModal + 180
20  UIKit                               0x0000000188069f70 UIApplicationMain + 204
21  myApp                               0x000000010013dd3c main + 124
22  libdyld.dylib                       0x000000018293a8b8 <redacted> + 4)

2016-04-14 15:16:49.700 myApp[1130:240689] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560'
*** First throw call stack:
(0x182ebee38 0x182523f80 0x182ec5ccc 0x182ec2c74 0x182dc0d1c 0x1001115a0     
0x10012c944 0x182ec4ae0 0x182dbc548 0x182dc0e70 0x100203370 0x182ec2aa4 
0x182dc0d1c 0x100188fec 0x183893ffc 0x182e75124 0x182e74bb8 0x182e728b8 
0x182d9cd10 0x184684088 0x188069f70 0x10013dd3c 0x18293a8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

我现在惊呆了。 初始初始化工作正常,多边形被绘制为应该,但是一旦我调用函数重绘多边形,崩溃发生..并且很奇怪现在我知道为什么它崩溃了,但我不明白..它将前5个阵列条目更改为GMSMutablePath和GMSPolyLine,而不是GMSPolygon?!,请参见下面的链接 ..并且不知道为什么,因为我100%肯定平均时间内其他任何地方都没有触及GMSPolygon数组。

Picture of of change in Array type

1 个答案:

答案 0 :(得分:0)

由于我的声誉,我无法发表评论。就像Dan所说,使用 NSArray NSMutableArray 来存储您的对象。然后做你的东西。

更新: 以下是我的建议:

  • 将所有例外添加到断点导航器中。

  • 尝试不用循环测试代码。

  • 尝试添加浮点值作为坐标。

请参阅下面的示例代码:

GMSMutablePath *prettyPoly = [GMSMutablePath path];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.506191, 1.83197)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.05249, 1.650696)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(51.92225, 1.321106)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(51.996719, 1.219482)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.049112, 1.244202)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.197507, 1.334839)];
[prettyPoly addCoordinate:CLLocationCoordinate2DMake(52.519564, 1.801758)];

GMSPolygon *polygon = [GMSPolygon polygonWithPath: prettyPoly];
polygon.fillColor = [UIColor colorWithRed:0 green:0.25 blue:0 alpha:0.3];
polygon.strokeColor = [UIColor greenColor];
polygon.strokeWidth = 5;
polygon.map = self.googleMap;

如果一切都失败了:   - 请参阅此文档:https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_polygon#properties