简体   繁体   English

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

[英]Redundant conformance of 'CLLocationCoordinate2D' to protocol 'decodable'/'encodable'

I'm writing on an interally used framework making use of CoreLocation. 我正在写一个使用CoreLocation的互用框架。 Don't ask me why but I have the requirement to make CLLocation codable. 不要问我为什么,但是我有要求使CLLocation编码。 So I came up with a struct 所以我想出了一个结构

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)
    }
}

Then I'm conforming to Codable in an extension to CLLocation . 然后,我在CLLocation的扩展中遵循Codable Putting data into that struct or pulling the data out of it. 将数据放入该结构或从中拉出数据。 To make this work, I also had to make CLLocationCoordinate2D conform to Codable . 为了使此工作有效,我还必须使CLLocationCoordinate2D符合Codable I did this by writing the following very sophisticated extension 为此,我编写了以下非常复杂的扩展

extenstion CLLocationCoordinate2D: Codable {}

Now I wanted to do things right for a change, so I wanted to get started writing unit tests. 现在,我想为更改做正确的事情,所以我想开始编写单元测试。 The problem is that my extension to CLLocationCoordinate2D needs to be part of both targets: the unit test and the framework itself. 问题是我对CLLocationCoordinate2D扩展需要成为两个目标的一部分:单元测试和框架本身。 Unfortunately this does not compile. 不幸的是,这无法编译。 It fails with 它失败了

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'Encodable'

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'Decodable' “ CLLocationCoordinate2D”与协议“可解码”的冗余一致性

Pointing out that CLLocationCoordinate2D already conforms to the protocol at the same code line. 指出CLLocationCoordinate2D已经符合同一代码行上的协议。 Building the target that depends on said frameworks works perfectly though. 但是,建立依赖于所述框架的目标是完美的。 Do you have any ideas to fix that? 您是否有解决办法?

Best, 最好,

geru 杰鲁

Your extension should not need to be part of the tests target. 您的扩展程序不必成为测试目标的一部分。

You import your main application target into the unit test with the @testable attribute 您可以使用@testable属性将主应用程序目标导入单元测试中

import XCTest

@testable import MyProject

class MyProjectViewControllerTests: XCTestCase {}

This allows you to use the classes, extensions etc from your project without adding them to the test target. 这使您可以使用项目中的类,扩展等,而无需将其添加到测试目标中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM