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时会崩溃。如何解决这个崩溃问题?
答案 0 :(得分:2)
这里有很多问题,但跳出列表顶部的问题是:
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"Address"]objectAtIndex:2] !=nil)
...
和
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:@"City"]objectAtIndex:3]!= nil) {
...
问题是objectAtIndex
数组的valueForKey
永远不会为nil
。您无法在数组中存储nil
,因此valueForKey
的作用是,如果找不到值,则使用NSNull
对象[NSNull null]
。这表示找不到任何值,但使用NSNull
(可以添加到数组中)而不是nil
(不能)。
问题可能是有一些后续代码(例如,尝试计算出标注气泡大小的代码)试图获取字符串的长度,但是因为你存储了{{1}它试图调用NSNull
方法并且它失败了。
您可以通过多种方式解决此问题,例如:
length