A custom init in the Swift class gives a property that is not initialized when the super.init call?

I am trying to make a simple class that will be used as annotations for mine MKMapView. I am extracting the data that I want to analyze in these annotation objects, and think that it would be nice to initialize the annotation with the dictionary and do all the parsing in this model class, not in UIViewController. It seems I can not get rid of errors related to compilers, although when I try to fix them, another appears.

Currently, getting a property is self.coordinatenot initialized with an implicitly generated call super.init. The call super.init()only creates another error, “Property is self.coordinatenot initialized in super.initcall,” regardless of where I make the call super.init()in the method. Any help could be helpful! Here is what my code looks like now.

import Foundation
import MapKit

class Student: NSObject, MKAnnotation {

var coordinate: CLLocationCoordinate2D
var title: String
var subtitle: String

init(dictionary: Dictionary<String, AnyObject>) {
    if let first = dictionary["firstName"] as? String {
        if let last = dictionary["lastName"] as? String {
            self.coordinate = CLLocationCoordinate2DMake(dictionary["latitude"] as! Double, dictionary["longitude"] as! Double)
            self.title = first + " " + last
            if let mediaURL = dictionary["mediaURL"] as? String {
                self.subtitle = mediaURL
            }
        }
    }
}
}
+4
source share
1 answer

In swift, you have most of the variables initialized to super.init, unless optionally declared, in your case you have two variables that need to be initialized because you initialize your variables inside the is swift statement, not sure if it will really initialize if failure if. Change your code to:

var coordinate: CLLocationCoordinate2D?
var title: String?
var subtitle: String?

, init. endo , / , : super.init()

,

+6

All Articles