Search code examples
swiftswiftuimapkitmkmapview

Half Lat/Long Delta with No Extension Necessary


I'm attempting to consolidate what I currently have as an extension into my struct MapView.

 extension MKCoordinateRegion {

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

Solution

  • I'm not sure if I understand your question correctly but I think the problem is here: 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
                .....
        }
    }
    

    If you need to declare them as private static property, add private keyword after static.

    Two Swift extension rules to mention here:

    1. can't declare stored property. compiler error: Extensions must not contain stored properties
    2. can't declare class property. compiler error: Class properties are only allowed within classes; use 'static' to declare a static property (like I used in answer code block)