2011-05-05 94 views
1

当我点击一个注释时,它假设给我看那个小窗口不是我的情况,注释在这里,但是当我点击一个应用程序时抛出一个异常,并且我认为这个代码使问题:问题与注释

for (int i=0; i<[array count]; i++) { 

      NSDictionary *stationEnCours=[array objectAtIndex:i]; 


      NSString *distance=[stationEnCours objectForKey:@"distance"]; 
      float lng = [[stationEnCours objectForKey:@"ssiphone_longitude"] floatValue]; 
      float lat = [[stationEnCours objectForKey:@"ssiphone_latitude"] floatValue]; 
      NSString *ensStation=[stationEnCours objectForKey:@"ssiphone_enseigne"]; 

      location2D = (CLLocationCoordinate2D){ .latitude = lat, .longitude = lng }; 
      MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease]; 
      [mapView addAnnotation:annotation]; 
      MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2}; 
      MKCoordinateRegion region={location2D,span}; 
      [mapView setRegion:region]; 
      [self.view addSubview:mapView]; 
      } 

和MyLocation类实现MKAnnotation协议,我有这个:

@implementation MyLocation 
@synthesize enseigneDeLaStation=_enseigneDeLaStation; 
@synthesize distanceVersLaStation=_distanceVersLaStation; 
@synthesize coordinate=_coordinate; 



-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate 
{ 
    if ((self = [super init])) { 
     _enseigneDeLaStation = [enseigneDeLaStation copy]; 
     _distanceVersLaStation = [distanceVersLaStation copy]; 
     _coordinate = coordinate; 
    } 
    return self; 
} 

- (NSString *)title { 
    return _enseigneDeLaStation; 
} 

- (NSString *)subtitle { 
    return _distanceVersLaStation; 
} 

- (void)dealloc 
{ 
    [_enseigneDeLaStation release]; 
    _enseigneDeLaStation = nil; 
    [_distanceVersLaStation release]; 
    _distanceVersLaStation = nil;  
    [super dealloc]; 
}  


@end 

请大家帮忙,THX提前:))

编辑 @Anna:我有解决你提到的点,但我仍然有同样的问题:

for (int i=0; i<[array count]; i++) { 
      //afficher sur la carte (map) 
      NSDictionary *stationEnCours=[array objectAtIndex:i]; 
      //NSString *adresseStation=[stationEnCours objectForKey:@"ssiphone_adresse"]; 

      //float distance = [[stationEnCours objectForKey:@"distance"] floatValue]; 
      NSString *distance=[stationEnCours objectForKey:@"distance"]; 
      NSLog(@"distance: %.1f",distance); 
      float lng = [[stationEnCours objectForKey:@"ssiphone_longitude"] floatValue]; 
      float lat = [[stationEnCours objectForKey:@"ssiphone_latitude"] floatValue]; 
      NSString *ensStation=[stationEnCours objectForKey:@"ssiphone_enseigne"]; 

      //CLLocationCoordinate2D location2D= {.latitude = lat, .longitude = lng}; 
      location2D = (CLLocationCoordinate2D){ .latitude = lat, .longitude = lng }; 
      MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease]; 
      [mapView addAnnotation:annotation]; 
      // MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2}; 
      // MKCoordinateRegion region={location2D,span}; 
      //[mapView setRegion:region]; 
      //[self.view addSubview:mapView]; 

      } 
      MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2}; 
      MKCoordinateRegion region={location2D,span}; 
      [mapView setRegion:region]; 
      [self.view addSubview:mapView]; 

异常堆栈:

2011-05-05 13:26:04.518[786:207] -[NSDecimalNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x6e6dca0 
2011-05-05 13:26:04.520[786:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x6e6dca0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x02915919 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x0272a5de objc_exception_throw + 47 
    2 CoreFoundation      0x0291742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x02887116 ___forwarding___ + 966 
    4 CoreFoundation      0x02886cd2 _CF_forwarding_prep_0 + 50 
    5 UIKit        0x00451608 -[UICalloutView setSubtitle:animated:] + 88 
    6 UIKit        0x004512d8 -[UICalloutView setSubtitle:] + 49 
    7 MapKit        0x023849de -[MKAnnotationContainerView _showBubbleForAnnotationView:bounce:scrollToFit:userInitiated:avoid:] + 527 
    8 MapKit        0x02385735 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:avoid:] + 651 
    9 MapKit        0x0237fb78 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:] + 144 
    10 MapKit        0x0235a891 -[MKMapView handleTap:] + 459 
    11 UIKit        0x005a2394 -[UIGestureRecognizer _updateGestureWithEvent:] + 727 
    12 UIKit        0x0059ebf3 -[UIGestureRecognizer _delayedUpdateGesture] + 47 
    13 UIKit        0x005a3486 _UIGestureRecognizerUpdateObserver + 637 
    14 CoreFoundation      0x028f6d1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27 
    15 CoreFoundation      0x0288b987 __CFRunLoopDoObservers + 295 
    16 CoreFoundation      0x02854c17 __CFRunLoopRun + 1575 
    17 CoreFoundation      0x02854280 CFRunLoopRunSpecific + 208 
    18 CoreFoundation      0x028541a1 CFRunLoopRunInMode + 97 
    19 GraphicsServices     0x02e8e2c8 GSEventRunModal + 217 
    20 GraphicsServices     0x02e8e38d GSEventRun + 115 
    21 UIKit        0x0033ab58 UIApplicationMain + 1160 
    22 myApp       0x000021f8 main + 102 
    23 myApp       0x00002189 start + 53 
) 
terminate called after throwing an instance of 'NSException' 
Program received signal: “SIGABRT”. 
(gdb) 

编辑1:

两个NSLogs:

NSLog(@"distance: %@", [distance class]); 
      NSLog(@"ensStation: %@", [ensStation class]); 

for环告诉我说:

2011-05-05 13:38:01.223 myApp[856:207] distance: NSCFNumber 
2011-05-05 13:38:01.223 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.224 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.224 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.225 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.225 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.226 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.226 myApp[856:207] distance: NSCFNumber 
2011-05-05 13:38:01.226 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.226 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.227 myApp[856:207] ensStation: NSCFString 
2011-05-05 13:38:01.227 myApp[856:207] distance: NSDecimalNumber 
2011-05-05 13:38:01.227 myApp[856:207] ensStation: NSCFString 
(gdb) 

编辑2: 嗨再次,与注释的问题是固定的,但我认为我得到了同样的异常,当我退出,然后回来观点:

2011-05-05 14:56:47.374 TopStation[1234:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x806b620> was mutated while being enumerated.(
    "<MyLocation: 0x806c910>", 
    "<MyLocation: 0x806c7e0>", 
    "<MyLocation: 0x806c930>", 
    "<MyLocation: 0x806c9d0>", 
    "<MyLocation: 0x806ca20>", 
    "<MyLocation: 0x806cc40>", 
    "<MyLocation: 0x806c990>" 
)' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x02915919 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x0272a5de objc_exception_throw + 47 
    2 CoreFoundation      0x029153d9 __NSFastEnumerationMutationHandler + 377 
    3 TopStation       0x00005691 -[StationsSurLaCarteViewController requestFinished:] + 343 
    4 TopStation       0x00019597 -[ASIHTTPRequest reportFinished] + 171 
    5 Foundation       0x000abe9a __NSThreadPerformPerform + 251 
    6 CoreFoundation      0x028f6d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    7 CoreFoundation      0x028552cb __CFRunLoopDoSources0 + 571 
    8 CoreFoundation      0x028547c6 __CFRunLoopRun + 470 
    9 CoreFoundation      0x02854280 CFRunLoopRunSpecific + 208 
    10 CoreFoundation      0x028541a1 CFRunLoopRunInMode + 97 
    11 GraphicsServices     0x02e8e2c8 GSEventRunModal + 217 
    12 GraphicsServices     0x02e8e38d GSEventRun + 115 
    13 UIKit        0x0033ab58 UIApplicationMain + 1160 
    14 TopStation       0x00002188 main + 102 
    15 TopStation       0x00002119 start + 53 
) 
terminate called after throwing an instance of 'NSException' 
Program received signal: “SIGABRT”. 
(gdb) 

编辑3: 嗨再次,我有每个视图时,会出现这么一次,我这样做是为了清理从注释地图(只要在地图上显示注释之前:

if(request.responseStatusCode==200) 
    { 

     for (id<MKAnnotation> annotation in mapView.annotations) { 
      [mapView removeAnnotation:annotation]; 
     } 
     //parse the response 
      //... 

所以异常是由于这个for``loop,我从代码删除它和所有工作正常,但是,我真的需要从注释每个时间t清洁地图的视图显示,是否有另一种方法呢? Thx提前:)

+1

的'setRegion',尤其是'addSubview' _inside_ for循环没有意义。 mapView是IBOutlet还是以编程方式创建?另外,什么是确切的例外? – Anna 2011-05-05 12:15:22

+0

嗨,请看看我的编辑,我已经修正了你的观点。 – Luca 2011-05-05 12:27:06

+1

在for循环中,在'NSString * ensStation = ...'行之后,添加这两个NSLog并让我们知道它说的是什么:NSLog(@“distance:%@”,[distance class]); NSLog(@“ensStation:%@”,[ensStation class]);' – Anna 2011-05-05 12:33:19

回答

1

问题是这样的线:

NSString *distance=[stationEnCours objectForKey:@"distance"]; 

按照NSLogs,在stationEnCours的 “距离” 对象是NSDecimalNumber(不是的NSString)。然后你将它传递给initWithAnnotation,它假定它是一个NSString。

当地图视图尝试显示标注泡泡时,它还假定字幕是一个NSString并尝试调用它的stringByStandardizingWhitespace并失败(因为它实际上是一个NSDecimalNumber)。

尝试改变上述行:

NSString *distance = [NSString stringWithFormat:@"%f", 
    [(NSDecimalNumber *)[stationEnCours objectForKey:@"distance"] doubleValue]]; 
+0

我不能感谢你足够的安娜,你只是救我:)))))0123))))) – Luca 2011-05-05 12:49:55

+0

嗨,同样的异常是当我退出视图,然后我回到它:(可以它是内存管理的泄漏,我应该释放'for'循环中使用的数组吗?但是我没有分配/初始化它 – Luca 2011-05-05 13:27:17

+0

不需要释放数组,它确实是一个完全相同的异常吗?以前,它当你点击一个注释时发生了,现在你退出视图时会出现异常 – Anna 2011-05-05 13:33:09