ios - 注释的问题

标签 ios mapkit mkannotation

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

  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 协议(protocol)的 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

请提前帮忙,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:

两个 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 : 再次嗨,注释的问题已解决,但我想当我退出并返回 View 时遇到了同样的异常:

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: 再次嗨,每次出现 View 时我都必须从注释中清除 map ,所以我这样做了(就在 map 上显示注释之前:

if(request.responseStatusCode==200)
   {   

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

所以异常是由于这个for``loop,我从代码中删除了它并且一切正常,但是,我真的不需要在每次显示 View 时清除注释中的 map , 还有另一种选择吗?提前致谢:)

最佳答案

问题是这一行:

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

根据 NSLogs,stationEnCours 中的“距离”对象是一个 NSDecimalNumber(不是 NSString)。然后将它传递给 initWithAnnotation,它假设它是一个 NSString。

本地图 View 试图显示标注气泡时,它还假定副标题是一个 NSString 并尝试对其调用 stringByStandardizingWhitespace 但失败了(因为它实际上是一个 NSDecimalNumber)。

尝试将上面的行更改为:

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

关于ios - 注释的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5897103/

相关文章:

ios - iCloud : NSMetadataQuery return wrong results when I delete files in setting device

swift - WatchOS InterfaceMap 更新用户在 map 上的位置

iphone - iOS - 如何使用 mkmapview 作为位置选择器

swift - 当其他函数中的循环完成运行时,如何在 swift 中调用函数?

ios - MKPointAnnotation 字幕

ios - 在 CollectionView 中使用 SDWebImage 的疯狂内存问题(在 tableviewCell 内)

ios - 是否可以在 Objective c 中打开正在运行的后台应用程序

iphone - 兴趣点 API/Web 服务

javascript - 文本链接到 <a href ='...' >//仅 JavaScript

ios - 调试 'CALayer position contains NaN: [nan nan]'