MKAnnotation protocol
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 im开发者_StackOverflow社区plementation
+ (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.
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.
精彩评论