Swift, Access parent properties in child class

1.9k views Asked by At

My problem is I don´t jnow how to access parent properties within the child class

class imageOverlayRenderer: MKOverlayRenderer {
var bar = self.overlay
}

MKOverlayRenderer should have the property overlay

What I really want is to write a custom subclass of MKOverlayRender to render an image, the code in Objective C is:

#import <MapKit/MapKit.h>

@interface PVParkMapOverlayView : MKOverlayRenderer

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage;

@end

and

#import "PVParkMapOverlayView.h"

@interface PVParkMapOverlayView ()

@property (nonatomic, strong) UIImage *overlayImage;

@end

@implementation PVParkMapOverlayView

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
    self = [super initWithOverlay:overlay];
    if (self) {
        _overlayImage = overlayImage;
    }

    return self;
}

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGImageRef imageReference = self.overlayImage.CGImage;

    MKMapRect theMapRect = self.overlay.boundingMapRect;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextDrawImage(context, theRect, imageReference);
}

@end
1

There are 1 answers

0
Nate Cook On

You can't reference self in a normal property declaration - you have to declare the property lazy:

class imageOverlayRenderer: MKOverlayRenderer {
    lazy var bar: MKOverlay! = self.overlay
}