我目前正在学习一个地理定位教程,该教程将MKAnnotation协议引入到一个类中。
本教程建议在Theannotation.h类中创建以下方法
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;并且在实现中
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord {
return [[[[self class] alloc] initWithCoordinate:coord] autorelease];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord {
if ( self = [super init] ) {
self.coordinate = coord;
}
return self;}
然后在视图控制器中调用第二个方法
Theannotation *annotation = [[SimpleAnnotation alloc] initWithCoordinate:Coords];我完全理解第二种方法,但是我对包含第一种方法感到困惑。在示例教程中的任何其他地方都没有调用类方法,我很难理解为什么在这种情况下会使用类方法。
发布于 2011-02-23 18:50:16
您可以省略这个类方法,但在某些情况下它很有用,因为它为您提供了一种创建“临时”注释的机制,该注释将自动发布。当然,您可以手动执行此操作,但在这种情况下,类方法是一种方便的方法。
发布于 2011-02-23 18:52:24
https://stackoverflow.com/questions/5089797
复制相似问题