问题
The code extension ARAnchor: Codable {}
produces the error:
"Implementation of 'Decodable' cannot be automatically synthesized in an extension in a different file to the type".
What does this mean? I was able to implement Codable
for another native type in a similar fashion without any errors.
回答1:
You could create a container object that implements Codable
and then use that to encode and decode the anchor. I tried this code in a playground and it work for me. You'll want to adapt it for which data you want from the anchor; for example I encoded name
but that might be useless to you and it might even break if your anchor was initialized without a name. You could also do the same thing with simd_float4x4
.
import Foundation
import ARKit
class AnchorContainer: Codable {
let anchor: ARAnchor
init(anchor: ARAnchor) {
self.anchor = anchor
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(String.self, forKey: .name)
let transform0 = try container.decode(simd_float4.self, forKey: .transform0)
let transform1 = try container.decode(simd_float4.self, forKey: .transform1)
let transform2 = try container.decode(simd_float4.self, forKey: .transform2)
let transform3 = try container.decode(simd_float4.self, forKey: .transform3)
let matrix = simd_float4x4(columns: (transform0, transform1, transform2, transform3))
anchor = ARAnchor(name: name, transform: matrix)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(anchor.name, forKey: .name) // Might want to make sure that the name is not nil here
try container.encode(anchor.transform.columns.0, forKey: .transform0)
try container.encode(anchor.transform.columns.1, forKey: .transform1)
try container.encode(anchor.transform.columns.2, forKey: .transform2)
try container.encode(anchor.transform.columns.3, forKey: .transform3)
}
enum CodingKeys: String, CodingKey {
case name
case transform0
case transform1
case transform2
case transform3
}
}
// EXAMPLE:
let anchor = ARAnchor(name: "Bill", transform: simd_float4x4(float4(repeating: 4), float4(repeating: 5), float4(repeating: 6), float4(repeating: 7))) // Make a arbitrary anchor
print(anchor) // Figure out what it's value is
do {
let data = try JSONEncoder().encode(AnchorContainer(anchor: anchor))
let anchorDecode = try JSONDecoder().decode(AnchorContainer.self, from: data)
print(anchorDecode.anchor) // Print the value after decoding to make sure that the result is the same
} catch {
print(error.localizedDescription)
}
回答2:
Synthesizing conformace to Codable
, Equatable
and Hashable
in different source files is currently not supported by the Swift compiler, AFAICT. See https://bugs.swift.org/browse/SR-6101 for the issue tracking this.
回答3:
Since Codable
is a type alias for the Encodable
and Decodable
protocols, when you use it as a type or a generic constraint, it matches any type that conforms to both protocols:
public typealias Codable = Decodable & Encodable
At the moment (Xcode 10.2.1 / Swift 5.0.1)
Codable
currently isn't supported yet if an extension in one file adds conformance in a different file. Check this out at https://bugs.swift.org/.
Hope this helps.
来源:https://stackoverflow.com/questions/55677651/implementing-codable-for-aranchor-cannot-be-automatically-synthesized-in-an-ex