iosswiftcore-locationswift-protocols

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'decodable'/'encodable'


I'm writing on an interally used framework making use of CoreLocation. Don't ask me why but I have the requirement to make CLLocation codable. 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. 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. 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. Unfortunately this does not compile. It fails with

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'Encodable'

Redundant conformance of 'CLLocationCoordinate2D' to protocol 'Decodable'

Pointing out that CLLocationCoordinate2D already conforms to the protocol at the same code line. Building the target that depends on said frameworks works perfectly though. Do you have any ideas to fix that?

Best,

geru


Solution

  • 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

    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.