0
votes

I'm trying (in playground) to make CLCircularRegion Codable. But I have an error that I cannot fix: error: initializer requirement 'init(from:)' can only be satisfied by a 'required' initializer in the definition of non-final class 'CLCircularRegion'. Any ideas?

import UIKit
import MapKit

// MARK: - CLLocationCoordinate2D

extension CLLocationCoordinate2D: Codable {

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(longitude)
        try container.encode(latitude)
    }

    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let longitude = try container.decode(CLLocationDegrees.self)
        let latitude = try container.decode(CLLocationDegrees.self)
        self.init(latitude: latitude, longitude: longitude)
    }
}

// MARK: - CLLocationCoordinate2D

extension CLCircularRegion : Codable {

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(identifier)
        try container.encode(center)
        try container.encode(radius)
    }

    public required init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let identifier = try container.decode(String.self)
        let center = try container.decode(CLLocationCoordinate2D.self)
        let radius = try container.decode(CLLocationDistance.self)

        self.init(center: center, radius: radius, identifier: identifier)
    }
}

let coordinate = CLLocationCoordinate2D(latitude: 45.123, longitude: 5.678)
let region = CLCircularRegion(center: coordinate, radius: 100, identifier: "identifierTest")

// encode
if let jsonData = try? JSONEncoder().encode(region), let jsonString = String(data: jsonData, encoding: .utf8) {
    print(jsonString)

    // decode
    if let decodedRegion = try? JSONDecoder().decode(CLCircularRegion.self, from: jsonData) {
        print(decodedRegion)
    }
}

2

2 Answers

1
votes

One solution is to wrap the class in a struct that conforms to Codable

struct CodableCircularRegion : Codable {
    var region: CLCircularRegion?
    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(region?.identifier)
        try container.encode(region?.center)
        try container.encode(region?.radius)
    }

    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let identifier = try container.decode(String.self)
        let center = try container.decode(CLLocationCoordinate2D.self)
        let radius = try container.decode(CLLocationDistance.self)

        region = CLCircularRegion(center: center, radius: radius, identifier: identifier)
    }

    init(_ region: CLCircularRegion) {
        self.region = region
    }
}

The encoding/decoding is almost the same

if let jsonData = try? JSONEncoder().encode(CodableCircularRegion(region)), let jsonString = String(data: jsonData, encoding: .utf8) {
    print(jsonString)

    // decode
    if let decodedRegion = try? JSONDecoder().decode(CodableCircularRegion.self, from: jsonData) {
        print(decodedRegion.region)
    }
}
1
votes

You can't do this. CLCircularRegion is not a final class. When you extend a non-final class you can only write convenience initializers in the extension. Decodable specifies that the initializer must be declared required.

But we just saw that you can't do that because Swift doesn't allow you to declare required initializers in an extension.

As an aside, in general it's not a good idea to extend somebody else's types to make them conform to Codable because at some future point whoever wrote the type might also add Codable conformance, and it's very likely that their implementation won't match your implementation, which will cause some bugs that will be very difficult to diagnose.

As mentioned, your best bet is to wrap the CLCircularRegion inside a struct that conforms to Codable.