-1

I want to convert a custom MKAnnotationView written in Obj-C to Swift and I am having an error when I want to initWithAnnotation.

Below I will provide both the ObjC and The Swift code:

class JPThumbnailAnnotationView: MKAnnotationView,JPThumbnailAnnotationViewProtocol {


var coordinate :CLLocationCoordinate2D
var imageView : UIImageView
var titleLabel : UILabel
var subtitleLabel : UILabel
var disclosureBlock : JPThumbnail.SequencerNext

func initWithAnnotation(annotation : MKAnnotation) {

    self = MKAnnotationView(annotation: annotation, reuseIdentifier: kJPSThumbnailAnnotationViewReuseID) //HERE IS WHERE I GET THE ERROR

        self.canShowCallout = true
        self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight)
        self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset)


}

And the Objective-C version is

- (id)initWithAnnotation:(id<MKAnnotation>)annotation {
  self = [super initWithAnnotation:annotation reuseIdentifier:kJPSThumbnailAnnotationViewReuseID];

  if (self) {
    self.canShowCallout = NO;
    self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight);
    self.backgroundColor = [UIColor clearColor];
    self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset);

    _state = JPSThumbnailAnnotationViewStateCollapsed;

    [self setupView];
}

return self;

}

The error I am getting is:Can not assign self in a method

IS there a way I can still assign self in my case or should I just create a variable of type MKAnnotationView assign it to the result of the initialiser and change the return type of the function OR is there another way I can do this?

tudoricc
  • 709
  • 1
  • 12
  • 31

1 Answers1

0

You are doing it wrong in init method, you should call super class initializer's like this,

func initWithAnnotation(annotation : MKAnnotation) {

   super.init(annotation: annotation, reuseIdentifier: kJPSThumbnailAnnotationViewReuseID) //HERE IS WHERE I GET THE ERROR
   self.canShowCallout = true
   self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight)
   self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset)
}
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • Sorry for the dumb question I am about to ask you but how does this initialise the self object and for some reason the init function doesn't appear and I really can't figure out why – tudoricc Sep 25 '14 at 08:31
  • any idea why I am not getting that form of init? – tudoricc Sep 25 '14 at 11:53