简体   繁体   English

编码/解码 C 带有不可编码组件的结构

[英]Encoding/decoding C Struct with components that are not encodable

I have a struct defined in a C header like:我在 C header 中定义了一个结构,例如:

struct ComputeParameters {
    float scale;
    vector_float4 rgba;
}

It has to be defined in C and not in Swift for a reason, so I can't just add add the Coding protocol to it.它必须在 C 中定义,而不是在 Swift 中定义,所以我不能只添加编码协议。

I need to be able store and restore the contents of instances of the struct - ideally from Swift, but I can make an Objective-C class to code it if necessary.我需要能够存储和恢复结构实例的内容——最好是从 Swift 开始,但如果需要,我可以制作 Objective-C class 来对其进行编码。

I tried to encode it with Obj-c:我尝试用 Obj-c 对其进行编码:

NSValue *structValue = [NSValue value:&computeParams withObjCType:@encode(struct ComputeParameters)];

But I get this error:但我得到这个错误:

Encoding of 'struct ComputeParameters' type is incomplete because 'vector_float4' (aka 'simd_float4') component has unknown encoding “struct ComputeParameters”类型的编码不完整,因为“vector_float4”(又名“simd_float4”)组件的编码未知

Other than encoding each and every component of the struct (there are like 3 dozen components), is there a better way?除了编码结构的每个组件(大约有 3 打组件)之外,还有更好的方法吗?

You can access the actual bytes of the struct and then convert those bytes to NSData but that ends up being very low level and machine dependent.您可以访问结构的实际字节,然后将这些字节转换为NSData ,但这最终会变得非常低级并且依赖于机器。 This is the one extreme of your options and may work and work well as long as you have the same underlying machine architecture.这是您选择的一个极端,只要您具有相同的底层机器架构,它就可以正常工作。 Then you can just as well code up the whole implementation in C and not use NSData/NSCoding at all.然后,您也可以在 C 中编写整个实现,并且根本不使用NSData/NSCoding

If you use NSCoding then it comes with its own set of limitations as you see and you need to work with what it gives you.如果您使用NSCoding ,那么它有自己的一组限制,您需要使用它给您的东西。 There are a few handy low level functions such as encodeInt64 but then you still end up working with the individual members of the struct.有一些方便的低级函数,例如encodeInt64 ,但您最终仍然使用结构的各个成员。 This is the other extreme where you encode the individual struct members for each struct.这是您为每个结构编码单个结构成员的另一个极端。

If you use Objective-C the middle ground can be to create a class for each struct along with code to get a struct from the class and to init a class from a struct. If you use Objective-C the middle ground can be to create a class for each struct along with code to get a struct from the class and to init a class from a struct. Then you need code to encode / decode each class.然后您需要代码来编码/解码每个 class。 This will probably be a lot of work but if this integrates well into your solution I think is the better / more maintainable option.这可能需要做很多工作,但如果它很好地集成到您的解决方案中,我认为这是更好/更易于维护的选择。

The most direct way forward would be to use the low level NSCoding functions but you still end up mapping each and every struct member to some low level function but at least without a class to facilitate the encoding.最直接的方法是使用低级NSCoding函数,但您仍然最终将每个结构成员映射到一些低级 function 但至少没有 class 来促进编码。 Then you can eg create an identifier for each struct and use that when you decode to know what struct you need to create and decode into.然后,您可以例如为每个结构创建一个标识符,并在您解码时使用它来了解您需要创建和解码的结构。

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

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