“ CLLocationCoordinate2D”与协议“可解码” /“可编码”的冗余一致性

时间:2018-11-16 15:18:01

标签: ios swift core-location swift-protocols

我正在写一个使用CoreLocation的互用框架。不要问我为什么,但是我有要求将CLLocation编码。所以我想出了一个

struct CLLocationEncodingStruct: Codable {
    let coordinate: CLLocationCoordinate2D
    let altitude: CLLocationDistance
    let horizontalAccuracy: CLLocationAccuracy
    let verticalAccuracy: CLLocationAccuracy
    let speed: CLLocationSpeed
    let course: CLLocationDirection
    let timestamp: Date

    public init(with location: CLLocation) {
        coordinate = location.coordinate
        altitude = location.altitude
        horizontalAccuracy = location.horizontalAccuracy
        verticalAccuracy = location.verticalAccuracy
        speed = location.speed
        course = location.course
        timestamp = location.timestamp
    }

    var location: CLLocation {
        return CLLocation(coordinate: coordinate, altitude: altitude, horizontalAccuracy: horizontalAccuracy, verticalAccuracy: verticalAccuracy, course: course, speed: speed, timestamp: timestamp)
    }
}

然后,我在Codable的扩展名中符合CLLocation。将数据放入该结构或从中拉出数据。为了使此工作有效,我还必须使CLLocationCoordinate2D符合Codable。为此,我编写了以下非常复杂的扩展程序

extenstion CLLocationCoordinate2D: Codable {}

现在,我想做一些正确的事情以进行更改,因此我想开始编写单元测试。问题是我对CLLocationCoordinate2D的扩展必须同时是两个目标的一部分:单元测试和框架本身。不幸的是,这无法编译。失败

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'Encodable'

“ CLLocationCoordinate2D”与协议“可解码”的冗余一致性

指出CLLocationCoordinate2D已符合同一代码行上的协议。但是,建立依赖于所述框架的目标是完美的。您有任何解决办法吗?

最好

geru

1 个答案:

答案 0 :(得分:1)

您的扩展程序不必成为测试目标的一部分。

您使用@testable属性将主应用程序目标导入单元测试

import XCTest

@testable import MyProject

class MyProjectViewControllerTests: XCTestCase {}

这使您可以使用项目中的类,扩展等,而无需将其添加到测试目标中。