iphone - 编码为 NSValue 结构数组

标签 iphone objective-c ios c opengl-es

我正在尝试将多维结构数组编码到 NSValue 中。

这是结构:

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    // Front
    {{1, -1, 0}, {1, 0, 0, 1}},
    {{1, 1, 0}, {1, 0, 0, 1}},
(etc...)

我需要编码为 NSValue 以将其存储在对象“Block”中。

这就是我要存储的内容:

Block *newBlock = [[Block alloc] init];
newBlock.blockVertices = [NSValue value:&Vertices withObjCType:@encode(Vertex)];

并取回它:

Vertex tempVertices[24];
[newBlock.blockVertices getValue:&tempVertices];

一切似乎都工作正常,然后我尝试比较这样的值:

for (int i = 0; i < 24; i++){
    NSLog(@"%f = %f", Vertices[i].Position[0], tempVertices[i].Position[0]);
    NSLog(@"%f = %f", Vertices[i].Position[1], tempVertices[i].Position[1]);
    NSLog(@"%f = %f", Vertices[i].Position[2], tempVertices[i].Position[2]);
    NSLog(@" ");
}

我得到了奇怪的结果,例如: -1.000000 = -1.998905

问题可能出在哪里?任何帮助将不胜感激。

最佳答案

您需要告诉@encode有多少Vertex结构正在被编码,例如:

[NSValue value:&Vertices withObjCType:@encode(Vertex[24])];

关于iphone - 编码为 NSValue 结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062702/

相关文章:

objective-c - 在 OSX 中编写 IDE 或文本编辑器

mysql - 将 iOS 应用程序中的照片存储到服务器中

ios - Swift delegate 不继承 NSObject

ios - 仍未针对 iPhone 6 和 iPhone 6 Plus 进行优化

ios - 从 Objective-C 转换为 Swift (TestFlight)

ios - 应用程序崩溃并显示消息 "Usage: leveldbutil command..."

iphone - 如何在 UIPageViewController 中的一页设置文本和另一页的图像

c# - 有没有办法在 C# 中为 iphone 制作应用程序

ios - 将 NSURLConnection 转换为 NSURLSessionUploadTask 示例

ios - 从数据/模糊使用中获取值(value)