下面的所有代码都是代码的近似值(无法发布我的公司的确切代码),它是逐行键入的,因此不会编译。
我的注释代码如下所示:
@interface MyFunkyAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinated2D coordinate;
NSString *identifier;
// Lots of other fields that can be displayed.
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *identifier;
// Lots of other properties as above.
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
@end
然后在我的实现中,我得到了如下东西:
@implementation MyFunkyAnnotation
@synthesize coordinate;
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky
{
[self setCoordinate:[newFunky coordinate]];
[self setIdentifier:[newFunky identifier]];
// Lots more updating of various properties.
}
@end
这很有效,所以我当然决定重新设计整个过程,并将很多信息封装到其他类中。
现在我的代码如下:
@interface MyFunkyAnnotation: NSObject <MKAnnotation>
{
SourceInfo_1 *source1; // Contains its own coordinate property;
SourceInfo_2 *source2; // Contains its own coordinate property;
SourceInfo_3 *source3; // Contains its own coordinate property;
}
@property (nonatomic, retain) SourceInfo_1 *source1;
@property (nonatomic, retain) SourceInfo_2 *source2;
@property (nonatomic, retain) SourceInfo_3 *source3;
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
-(CLLocationCoordinate2D)coordinate;
@end
新的实施:
@implementation MyFunkyAnnotation
@synthesize source1, source2, source3;
-(CLLocationCoordinate2D)coordinate
{
if ([source1 dataReliable] == YES)
{
return [source1 coordinate];
}
else if ([source2 dataReliable] == YES)
{
return [source2 coordinate];
}
else
{
return [source3 coordinate];
}
}
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
{
if ([newFunky source1] != nil)
{
[self setSource1:[newFunky source1];
}
if ([newFunky source2] != nil)
{
[self setSource2:[newFunky source2];
}
if ([newFunky source3] != nil)
{
[self setSource3:[newFunky source3];
}
}
@end;
运行此新代码会将注释添加到原始位置,但它们根本不会移动。当得到XML提要时,我正在更新source1、source2、source3中的坐标。
经过一整天的调试和尝试,我终于成功了。然后,我开始删除我在一天中添加的所有内容,以获得以下最小的更改,使其生效,结果非常奇怪:
在实现中,我添加了一个方法,并添加了三行:
@implementation MyFunkyAnnotation
@synthesize source1, source2, source3;
-(void)setCoordinate:(CLLocationCoordinate2D)coordinate // <-- New method
{
nil;
}
-(CLLocationCoordinate2D)coordinate
{
if ([source1 dataReliable] == YES)
{
return [source1 coordinate];
}
else if ([source2 dataReliable] == YES)
{
return [source2 coordinate];
}
else
{
return [source3 coordinate];
}
}
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
{
if ([newFunky source1] != nil)
{
[self setSource1:[newFunky source1];
[self setCoordinate:[[newFunky source1] coordinate]]; // <--- New Line
}
if ([newFunky source2] != nil)
{
[self setSource2:[newFunky source2];
[self setCoordinate:[[newFunky source2] coordinate]]; // <--- New Line
}
if ([newFunky source3] != nil)
{
[self setSource3:[newFunky source3];
[self setCoordinate:[[newFunky source3] coordinate]]; // <--- New Line
}
}
@end;
有谁能解释为什么需要调用一个实际上什么都不做的方法。“nil”是NSLog语句,所以我知道它被调用了。这完全没有道理。
任何洞察都将不胜感激。
干杯,
布雷特