一半 Lat/Long 增量,无需扩展

Half Lat/Long Delta with No Extension Necessary

我正在尝试将我目前拥有的 extension 整合到我的 struct MapView 中。

 extension MKCoordinateRegion {

    var boundingBoxCoordinates: [CLLocationCoordinate2D] {
        let halfLatDelta = self.span.latitudeDelta / 2
        let halfLngDelta = self.span.longitudeDelta / 2

我不确定我是否正确理解了你的问题,但我认为问题出在这里:self.span in MKCoordinateRegion extension。

extension MKCoordinateRegion {

    static let latitudeDelta: CLLocationDegrees = 1
    static let longitudeDelta: CLLocationDegrees = 1

        var boundingBoxCoordinates: [CLLocationCoordinate2D] {
            let halfLatDelta = MKCoordinateRegion.latitudeDelta / 2
            let halfLngDelta = MKCoordinateRegion.longitudeDelta / 2
            .....
    }
}

如果需要将它们声明为私有静态属性,请在static后添加private关键字。

两个 Swift extension 规则要在此处提及:

  1. 无法声明已存储 属性。编译器错误:扩展不能包含存储的属性
  2. 无法声明 class 属性。编译器错误:Class properties are only allowed within classes;使用 'static' 声明静态 属性(就像我在答案代码块中使用的那样)