objective-c 创建结构数组的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/393151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the easiest way to create an array of structs?
提问by Steph Thirion
What's the easiest way to create an array of structs in Cocoa?
在 Cocoa 中创建结构数组的最简单方法是什么?
回答by Ashley Clark
If you want to use an NSArray you'll need to box up your structs. You can use the NSValue class to encode them.
如果您想使用 NSArray,则需要将结构装箱。您可以使用 NSValue 类对它们进行编码。
Something like this to encode:
像这样编码:
struct foo {
int bar;
};
struct foo foobar;
foobar.bar = 3;
NSValue *boxedFoobar = [NSValue valueWithBytes:&foobar objCType:@encode(struct foo)];
And then to get it back out:
然后把它弄回来:
struct foo newFoobar;
if (strcmp([boxedFoobar objCType], @encode(struct foo)) == 0)
[boxedFoobar getValue:&newFoobar];
回答by Georg Sch?lly
Or dynamically allocating:
或者动态分配:
struct foo {
int bar;
};
int main (int argc, const char * argv[]) {
struct foo *foobar = malloc(sizeof(struct foo) * 3);
foobar[0].bar = 7; // or foobar->bar = 7;
free(foobar);
}
回答by Rich Catalano
If you're okay with a regular old C array:
如果您对常规的旧 C 数组没问题:
struct foo{
int bar;
};
int main (int argc, const char * argv[]) {
struct foo foobar[3];
foobar[0].bar = 7;
}
If you want an NSArray, you'll need a wrapper object. Most primitives use NSNumber, but that might not be applicable to your struct. A wrapper wouldn't be very hard to write, although that kind of defeats the purpose of using a struct!
如果你想要一个 NSArray,你需要一个包装对象。大多数原语使用 NSNumber,但这可能不适用于您的结构。包装器不会很难编写,尽管那样会违背使用结构的目的!
Edit: This is something I haven't done but just thought of. If you think about it, an NSDictionary is basically a struct for objects. Your keys can be the names of the struct components as NSStrings and your values can be of the corresponding data type wrapped in the applicable Cocoa wrapper. Then just put these dictionaries in an NSArray.
编辑:这是我没有做过但只是想到的事情。如果你仔细想想,一个 NSDictionary 基本上是一个对象结构。您的键可以是结构组件的名称作为 NSStrings 并且您的值可以是包装在适用的 Cocoa 包装器中的相应数据类型。然后只需将这些字典放在 NSArray 中。
I guess the gist is that you've got plenty of options. If I were you I'd do some experimenting and see what works best.
我想要点是你有很多选择。如果我是你,我会做一些实验,看看什么最有效。
回答by Peter Hosey
Does it need to be a struct? It's generally better to make it an object if you can. Then you can use things like KVC and Bindings with it.
它需要是一个结构吗?如果可以,通常最好将其设为对象。然后你可以使用 KVC 和 Bindings 之类的东西。
回答by Alexander
It is easier to use class, but what if you need to work with C and objective c structs like CGRect or others. I've tried to use NSValue,but it work strange...:
使用类更容易,但是如果您需要使用 C 和目标 c 结构(如 CGRect 或其他),该怎么办。我试过使用 NSValue,但它的工作很奇怪......:
CGRect rect =CGRectMake(20,220,280,30);
NSValue* rectValue = [NSValue valueWithCGRect:rect];
NSArray *params1;
params1= [NSArray arrayWithObjects:rectValue,nil];
But,I can get CGRect value only as:
但是,我只能获得 CGRect 值:
CGRect cgr = [[params1 objectAtIndex:0] CGRectValue];
When I use this:
当我使用这个时:
id currentVal = [params1 objectAtIndex:0];
void* buffer;
buffer=[currentVal pointerValue];
it causes the error...
它导致错误...
problem solved here
问题在这里解决

