for (int i = 0; i< [delarsInfoArray count] ; i++)
{
    NSString *lattitudeValue;
    NSString *longitudeValue;
    if ([[delarsInfoArray objectAtIndex:i]count]>1) {
        lattitudeValue = [[[delarsInfoArray objectAtIndex:i]valueForKey:@"LATITUDE"]objectAtIndex:1];
        longitudeValue = [[[delarsInfoArray objectAtIndex:i]valueForKey:@"LONGITUDE"]objectAtIndex:0];
    }
    else
    {
        lattitudeValue = @"";
        longitudeValue = @"";
    }
    CLLocationCoordinate2D pinLocation;
    if(([lattitudeValue floatValue] != 0) && ([longitudeValue floatValue] != 0) ) {
        mapRegion.center.latitude = [lattitudeValue floatValue];
        mapRegion.center.longitude = [longitudeValue floatValue];
        if(pinLocation.latitude !=0 && pinLocation.longitude !=0) {
            myAnnotation1 = [[MyAnnotation alloc] init];
            if ([[delarsInfoArray objectAtIndex:i] count] == 0) {

                myAnnotation1.title  = @"";
                myAnnotation1.subtitle = @"";
            }
            else
            {
                // NSLog(@"====== delears array is===%@",delarsInfoArray);
                NSLog(@"===== delears array count is %d",[delarsInfoArray count]);

                if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"Address"]objectAtIndex:2] !=nil)
                {
                    myAnnotation1.title = [[[delarsInfoArray objectAtIndex:i]valueForKey:@"Address"]objectAtIndex:2];
                }
                if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"City"]objectAtIndex:3]!= nil) {
                    myAnnotation1.subtitle = [[[delarsInfoArray objectAtIndex:i]valueForKey:@"City"]objectAtIndex:3];
                }

                NSLog(@"%@",[[[delarsInfoArray objectAtIndex:i]valueForKey:@"City"]objectAtIndex:3]);
            }

            [dealerMapView setRegion:mapRegion animated:YES];
            [dealerMapView addAnnotation:myAnnotation1];
            myAnnotation1.coordinate = mapRegion.center;
            [myAnnotation1 release];
        }
    }
}

上面的代码写在viewWillAppear中。将地图加载到视图中后,当我单击map.app时崩溃了。如何解决此崩溃?

最佳答案

这里有很多问题,但是跳到列表顶部的是以下内容:

if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"Address"]objectAtIndex:2] !=nil)
    ...


if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"City"]objectAtIndex:3]!= nil) {
    ...

问题是数组的objectAtIndexvalueForKey永远不会是nil。您无法将nil存储在数组中,因此valueForKey会执行以下操作(如果找不到值),是否使用NSNull对象[NSNull null]。这表示未找到任何值,但是使用NSNull(可以将其添加到数组中)而不是nil(不能)。

问题可能是有一些后续代码(例如,试图弄清楚标注气泡大小的代码)试图获取字符串的长度,但是由于您存储了NSNull,因此它正在尝试调用length方法,并且失败。

您可以通过多种方式解决此问题,例如:
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"Address"]objectAtIndex:2] != [NSNull null])
    ...

关于ios - 当我单击 map 时,它在iOS5的初始时间就崩溃了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15585976/

10-12 00:15