无法为类型调用初始化程序...仅在编译测试时

时间:2017-05-11 19:17:03

标签: swift swift3

我有一个类有一些init方法,并且在添加一个新变量并因此添加了一个新的init之后,我开始只在我编译运行单元测试时才得到以下错误(我可以编译并运行我的应用程序没有收到该错误):

Overloads for 'SearchedLocation' exist with these partially matching parameter lists: (invocation: NSInvocation?), (selector: Selector), ()

我的课程是:

class SearchedLocation : NSObject, MKAnnotation {
    let coordinate: CLLocationCoordinate2D
    let address : String?
    let neighborhood : String?
    let city : String
    let fullAddress : String?
    let name : String?

//    ....

init(coordinate: CLLocationCoordinate2D, address: String, neighborhood: String, city: String, fullAddress: String) {
    self.coordinate = coordinate
    self.address = address
    self.neighborhood = neighborhood
    self.city = city
    self.fullAddress = fullAddress
    self.name = ""
}

init(coordinate: CLLocationCoordinate2D, address: String, neighborhood: String, city: String, fullAddress: String, name: String?) {
    self.coordinate = coordinate
    self.address = address
    self.neighborhood = neighborhood
    self.city = city
    self.fullAddress = fullAddress

    if name != nil && !name!.isEmpty {
        if self.fullAddress!.contains(name!) {
            self.name = ""
        } else {
            self.name = name!
        }
    } else {
        self.name = ""
    }
}

这就是我收到错误的地方:

let place = SearchedLocation(coordinate: (placemark?.coordinate)!, address: address, neighborhood: placemark!.subLocality!, city: placemark!.locality!, fullAddress: fullAddress)

该代码工作得很好(即使在运行测试时),直到我将最后一个init方法添加到类中。现在即使我发表评论,我仍然会收到该错误。

任何人都知道它是什么?

1 个答案:

答案 0 :(得分:2)

在撤消我的更改后,我意识到我已经创建了一个名为" SearchedLocation"的测试类,就像我的班级一样。这就是为什么我只在编译运行测试时才得到错误。

愚蠢的错误:(

相关问题