MKAnnotation协议

发布于 2024-10-18 17:51:22 字数 768 浏览 2 评论 0原文

我目前正在学习地理定位教程,该教程将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-10-25 17:51:22

您可以省略此类方法,但在某些情况下它很有用,因为它为您提供了一种创建将自动释放的“临时”注释的机制。当然,您可以手动执行此操作,但在这种情况下,类方法是一种方便的方法。

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.

郁金香雨 2024-10-25 17:51:22

请浏览此博客此处

或者你可以下载代码-
链接

并查看代码,您就会知道哪些事情是强制性的,哪些不是。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文