Swift,在子类中访问父级属性 [英] Swift, Access parent properties in child class

查看:66
本文介绍了Swift,在子类中访问父级属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我不知道如何访问子类中的父属性

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

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

MKOverlayRenderer应该具有属性叠加层

MKOverlayRenderer should have the property overlay

我真正想要的是编写一个MKOverlayRender的自定义子类来渲染图像,Objective C中的代码是:

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

#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

推荐答案

您不能在常规属性声明中引用 self -您必须声明属性 lazy :

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
}

这篇关于Swift,在子类中访问父级属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆