【发布时间】:2012-06-16 05:52:30
【问题描述】:
通过以下方式从我的地图视图中删除注释:
if ([[self.mapView annotations] count] > 0)
{
[self.mapView removeAnnotations:[self.mapView annotations]];
}
导致我的应用程序崩溃并出现以下异常:
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'
注解的添加方式如下:
CLLocationCoordinate2D pinPosition;
for (int index = 0; index < [array count]; index++)
{
Station *aStation = [array objectAtIndex:index];
PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
stationPin.stationName = [aStation valueForKey:@"stationName"];
stationPin.stationPosition = pinPosition;
stationPin.stationLength = [aStation valueForKey:@"platformLength"];
[self.mapView addAnnotation:stationPin];
[stationPin release];
}
我的 PFAnnotation.h 是:
@interface PFAnnotation : NSObject <MKAnnotation>
{
NSString *stationName;
CLLocationCoordinate2D stationPosition;
NSNumber *stationLength;
}
@property (nonatomic, retain) NSString *stationName;
@property CLLocationCoordinate2D stationPosition;
@property (nonatomic, retain) NSNumber *stationLength;
@end
我的 PFAnnotation.m 是:
@implementation PFAnnotation
@synthesize stationName;
@synthesize stationPosition;
@synthesize stationLength;
- (CLLocationCoordinate2D)coordinate;
{
return stationPosition;
}
- (NSString *)title
{
return stationName;
}
- (NSString *)subtitle
{
if (stationLength == nil)
return nil;
else
return [NSString stringWithFormat:@"Platform Length: %@ft",stationLength];
}
- (void)dealloc {
[stationName release];
[stationLength release];
[super dealloc];
}
我在其他一些线程中读到,从后台线程设置注释属性是上述错误的原因。但在我的情况下,情况并非如此,因为每件事都是在主线程上执行的。请指教。
【问题讨论】:
-
请先添加一些代码如何添加注释。!谢谢!
-
@Guntis Treulands 我已经编辑了问题并添加了代码。请看。
-
不设置stationName和stationLength会怎样?它还会崩溃吗?
-
好像不设置stationName就不会crash。 (从我目前测试的结果来看)。
-
这是必不可少的。你永远不应该对 NSString 属性使用保留。 NSArray、NSData、NSDictionary 或任何其他具有可变版本的类也是如此。请参阅此处了解原因:stackoverflow.com/questions/387959/…
标签: iphone ios xcode cocoa-touch ios5