为ARAnchor实现Codable:“无法在扩展程序中自动合成..." [英] Implementing Codable for ARAnchor: "cannot be automatically synthesized in an extension..."

查看:142
本文介绍了为ARAnchor实现Codable:“无法在扩展程序中自动合成..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码extension ARAnchor: Codable {}会产生错误:

不能在与该类型不同的文件的扩展名中自动合成'Decodable'的实现."

"Implementation of 'Decodable' cannot be automatically synthesized in an extension in a different file to the type".

这是什么意思?我能够以类似的方式为另一个本机类型实现Codable,而没有任何错误.

What does this mean? I was able to implement Codable for another native type in a similar fashion without any errors.

推荐答案

您可以创建一个实现Codable的容器对象,然后使用该对象来编码和解码锚点.我在操场上尝试了此代码,它对我有用.您将需要对其进行调整,以适应来自锚点的所需数据.例如,我对name进行了编码,但是这对您可能没有用,并且如果您的锚点初始化时没有名称,它甚至可能会中断.您也可以使用simd_float4x4做同样的事情.

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

这篇关于为ARAnchor实现Codable:“无法在扩展程序中自动合成..."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆