Swift说我有一个额外的初始化器参数。为什么?

时间:2014-11-12 21:15:27

标签: swift initialization

为什么我无法使用父类初始值设定项?

这是我正在关注的API:

enter image description here

这是错误:

enter image description here

这是代码:

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
    }

}

我在这里缺少什么?


我根据反馈进行了更改;但是编译器仍然坚持我有所需的init():

enter image description here

所以通过编译器提示进行了一些更改,并提出了这个:

enter image description here

编译器很开心;但是必须插入解码器init()似乎很尴尬。

这是实例化MKAnnotationView对象的正确方法吗?

1 个答案:

答案 0 :(得分:2)

问题是您已经实现了指定的初始值设定项init(coder:),这是您已实现的初始值设定项。因此,斯威夫特抱怨说这就是你必须要的。我认为您可以通过在子类实现中重新声明init(annotation:reuseIdentifier:)来解决这个问题,即使它只是调用super