Frederick C. Lee
Frederick C. Lee

Reputation: 9523

Swift says I have an extra initializer argument. Why?

Why am I unable to use a parent-class initializer?

Here's the API that I'm following:

enter image description here

Here's the error:

enter image description here

Here's the code:

import Foundation
import UIKit
import MapKit

class UserInMapView:MKAnnotationView {

    required init(coder aDecoder: NSCoder) {
        println("{UserInMapView} init coder.")
        super.init(coder: aDecoder)
    }

    func doSomething() {
        let imageView = UIImageView(frame: CGRectMake(-20, -20, 50, 50))
        imageView.contentMode = .ScaleAspectFit;
        imageView.layer.cornerRadius = 25.0;
        imageView.layer.masksToBounds = true;
        imageView.layer.borderColor = UIColor.whiteColor().CGColor;
        imageView.layer.borderWidth = 1.0;
        self.addSubview(imageView)
    }
}

// =======================================================================================================================

class PinpointMap:NSObject, MKAnnotation {
    var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.3315644,-122.0296575)
    var profilePhoto:UIImage
    var userID:String?

    init(userID:String, profilePhoto:UIImage) {
        self.userID = userID
        self.profilePhoto = profilePhoto
    }


    func getAnnotationView() -> MKAnnotationView {
        // Using MKAnnotationView protocol:
        let thisView = UserInMapView(annotation: self, reuseIdentifier: "UserInMap")
        //        this.profileImage.image = profilePhoto
        return thisView
    }

}

What am I missing here?


I've made changes per feedback; but the compiler still insist that I have the required init():

enter image description here

So made some changes via compiler hints and came up with this:

enter image description here

The compiler's happy; but it seems awkward to have to plug in the decoder init().

Is this the correct way to instantiate a MKAnnotationView object?

Upvotes: 1

Views: 466

Answers (1)

matt
matt

Reputation: 536009

The problem is that you have implemented a designated initializer init(coder:) and that is the only initializer you have implemented. Hence, Swift is complaining that that is what you must call. I think you can work around this by also redeclaring init(annotation:reuseIdentifier:) in your subclass implementation, even if all it does is call super.

Upvotes: 2

Related Questions