在 Mapbox iOS SDK 3.3.1 中继承 MGLPolygon

Inherit MGLPolygon in Mapbox iOS SDK 3.3.1

我正在尝试定义继承 MGLPolygon 的 class 建筑。 MGLPolygon 定义为:

public class MGLPolygon : MGLMultiPoint, MGLOverlay {

    public var interiorPolygons: [MGLPolygon]? { get }

    public convenience init(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>, count: UInt)

    public convenience init(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>, count: UInt, interiorPolygons: [MGLPolygon]?)
}

MGLPolygon 的指定初始化程序隐藏在 swift 版本的 SDK 中。以下将失败:

class Building: MGLPolygon {

    let name: String

    init(name: String, coordinates: [CLLocationCoordinate2D]){
        self.name = name
        super.init(coordinates: &coordinates, count: UInt(coordinates.count))
        // Must call a designated initializer of the superclass 'MGLPolygon'
    }
}

我检查了 Objective-C 中的 original SDK code:

@implementation MGLPolygon

@dynamic overlayBounds;

+ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count {
    return [self polygonWithCoordinates:coords count:count interiorPolygons:nil];
}

+ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
    return [[self alloc] initWithCoordinates:coords count:count interiorPolygons:interiorPolygons];
}

- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
    if (self = [super initWithCoordinates:coords count:count]) {
        if (interiorPolygons.count) {
            _interiorPolygons = interiorPolygons;
        }
    }
    return self;
}

- (mbgl::LinearRing<double>)ring {
    NSUInteger count = self.pointCount;
    CLLocationCoordinate2D *coordinates = self.coordinates;

    mbgl::LinearRing<double> result;
    result.reserve(self.pointCount);
    for (NSUInteger i = 0; i < count; i++) {
        result.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
    }
    return result;
}

- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
    mbgl::Polygon<double> geometry;
    geometry.push_back(self.ring);
    for (MGLPolygon *polygon in self.interiorPolygons) {
        geometry.push_back(polygon.ring);
    }

    mbgl::FillAnnotation annotation { geometry };
    annotation.opacity = [delegate alphaForShapeAnnotation:self];
    annotation.outlineColor = [delegate strokeColorForShapeAnnotation:self];
    annotation.color = [delegate fillColorForPolygonAnnotation:self];

    return annotation;
}

@end

但是,不幸的是,我对 Objective-C 并不擅长,而且我不理解代码。

我在问什么?

Swift中MGLPolygon的指定初始化器是什么?它以什么作为参数?

附加问题

为什么隐藏了指定的初始化程序?

我认为您需要在子class(例如+buildingWithCoordinates:count:)中创建一个class方法,它调用super的实现来处理这个问题.

根据@incanus 的建议我的解决方案:

class Building: MGLPolygon {

    var name: String?

    afterInit(name: String){
        self.name = name
    }
}

func initBuilding(name: String, coordinates: [CLLocationCoordinate2D]) -> Building {
    let building = Building(coordinates: &coordinates, count: UInt(coordinates.count))
    building.afterInit(name)
    return building
}     

虽然不优雅,但很管用。