注释问题

时间:2011-05-05 11:43:40

标签: ios mapkit mkannotation

当我点击一个注释时,它会向我显示那个不是我的情况的小窗口,注释在这里,但是当我点击一个应用程序抛出异常时,我认为这个代码会产生问题:< / p>

  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];
           }

对于实现MKAnnotation协议的MyLocation类,我有这个:

@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

请提前帮助:)))

修改 @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:

两个NSLog:

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,我从代码中删除它并且一切正常,但是,每次显示视图时我都需要从注释中清除地图,是否有另一种选择这样做 ??提前thx:)

1 个答案:

答案 0 :(得分: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]];