MKAnnotation协议
我目前正在学习地理定位教程,该教程将 MKAnnotation 协议纳入类中。
创建以下方法
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;
本教程建议在 Theannotation.h 类和实现中
+ (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];
我完全理解第二个方法,但是我对包含第一个方法感到困惑。示例教程中的任何其他地方都没有调用类方法,我很难理解为什么在这种情况下要使用类方法。
Im currrently going through a geolocation tutorial which adopts the MKAnnotation Protocol into a class.
The tutorial suggests to create the following methods in the Theannotation.h class
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;
and in the implementation
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord {
return [[[[self class] alloc] initWithCoordinate:coord] autorelease];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord {
if ( self = [super init] ) {
self.coordinate = coord;
}
return self;
}
The second method is then called in a viewcontroller
Theannotation *annotation = [[SimpleAnnotation alloc] initWithCoordinate:Coords];
I understand the second method completely however Im puzzled to the inclusion of the first. The class method isn't called at any other place in the example tutorial and im struggling to understand why you would use a class method in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以省略此类方法,但在某些情况下它很有用,因为它为您提供了一种创建将自动释放的“临时”注释的机制。当然,您可以手动执行此操作,但在这种情况下,类方法是一种方便的方法。
You can omit this class method but in some cases it is useful because it provides you a mechanism to create 'temporary' annotation that will be autoreleased. Of course you can do it manually, but class method is a way of convenience in that case.
请浏览此博客此处
或者你可以下载代码-
链接
并查看代码,您就会知道哪些事情是强制性的,哪些不是。
please go through this blog here
or you can download the code-
link
and see the code, you will know that which things are mandatory and which will not.