Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 斯威夫特说我有一个额外的初始化参数。为什么?_Swift_Initialization - Fatal编程技术网

Swift 斯威夫特说我有一个额外的初始化参数。为什么?

Swift 斯威夫特说我有一个额外的初始化参数。为什么?,swift,initialization,Swift,Initialization,为什么我不能使用父类初始值设定项 下面是我要遵循的API: 以下是错误: 代码如下: import Foundation import UIKit import MapKit class UserInMapView:MKAnnotationView { required init(coder aDecoder: NSCoder) { println("{UserInMapView} init coder.") super.init(coder:

为什么我不能使用父类初始值设定项 下面是我要遵循的API:

以下是错误:

代码如下:

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():

因此,通过编译器提示进行了一些更改,并得出以下结论:

编辑很高兴;但插入解码器init()似乎很尴尬。
这是实例化MKAnnotationView对象的正确方法吗?

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

我认为问题是您需要在MKAnnotationView子类中实现
init(annotation:reuseIdentifier:)
,即使你所做的只是在它里面调用
super
。如果我的猜测是正确的,你可能会想向苹果提交一份bug报告,因为这看起来并不是正确的行为。初始值设定项继承变得非常棘手,并且您得到的错误消息远远没有帮助……我仍然需要处理所需的init(编码器…)。我将提交一份bug报告供苹果反馈。是的。。。我必须重新声明init()。但我还必须处理init(代码…)。我将向苹果公司提交错误报告,征求他们的反馈意见。我提交了错误报告:#18961079。这不是一个错误-Swift类只有在满足某些条件时才会自动继承其超类的初始值设定项。请参阅中的“自动初始值设定项继承”。错误不是继承,@NateCook。这是因为编译器的抱怨毫无帮助。哦,那就继续吧。这些错误信息是一刀切的。