Lastmboy
Lastmboy

Reputation: 1869

Swift super.init() - Property not initialized at super.init call

I'm getting the error "Property 'self.directionsCompletionHandler' not initialized at super.init call" on the super.init() line. This worked fine right up until the most recent Xcode update (11.4). Removing the init() or super.init() also cause errors. I'm not quite sure what it wants me to do.

import UIKit
import CoreLocation
import MapKit

typealias DirectionsCompletionHandler = ((_ route:MKPolyline?, _ directionInformation:NSDictionary?, _ boundingRegion:MKMapRect?, _ error:String?)->())?

class MapManager: NSObject{

    fileprivate var directionsCompletionHandler:DirectionsCompletionHandler
    fileprivate let errorNoRoutesAvailable = "No routes available"// add more error handling

    override init(){
        super.init()
    }

    ...

Upvotes: 0

Views: 1773

Answers (2)

Rob
Rob

Reputation: 437582

I would suggest not making the typealias, itself, optional, but rather just a simple closure:

typealias DirectionsCompletionHandler = (_ route: MKPolyline?, _ directionInformation: NSDictionary?, _ boundingRegion: MKMapRect?, _ error: String?) -> Void

This is the standard convention when defining typealias for a closure.

And then define your directionCompletionHandler to make the optional behavior explicit:

fileprivate var directionsCompletionHandler: DirectionsCompletionHandler?

And the compiler has no trouble figuring out that it doesn’t need to be initialized.

Or, of course, you could make this initialization explicit if you wanted:

fileprivate var directionsCompletionHandler: DirectionsCompletionHandler? = nil

Upvotes: 1

dasdom
dasdom

Reputation: 14063

Replace

fileprivate var directionsCompletionHandler:DirectionsCompletionHandler

with

fileprivate var directionsCompletionHandler: DirectionsCompletionHandler = nil

Upvotes: 1

Related Questions