什么是最简单的方法来创建结构体数组? [英] What's the easiest way to create an array of structs?

查看:170
本文介绍了什么是最简单的方法来创建结构体数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cocoa中创建结构体数组最简单的方法是什么?

What's the easiest way to create an array of structs in Cocoa?

推荐答案

如果你想使用NSArray,我们需要装箱你的结构。您可以使用NSValue类对它们进行编码。

If you want to use an NSArray you'll need to box up your structs. You can use the NSValue class to encode them.

这样编码的东西:

struct foo {
    int bar;
};

struct foo foobar;
foobar.bar = 3;
NSValue *boxedFoobar = [NSValue valueWithBytes:&foobar objCType:@encode(struct foo)];

然后将其还原出来:

struct foo newFoobar;

if (strcmp([boxedFoobar objCType], @encode(struct foo)) == 0)
    [boxedFoobar getValue:&newFoobar];

这篇关于什么是最简单的方法来创建结构体数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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