What's the easiest way to create an array of structs in Cocoa?
用Cocoa创建结构数组最简单的方法是什么?
5 个解决方案
#1
44
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];
#2
20
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);
}
#3
11
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基本上就是对象的结构体。您的键可以是结构组件的名称,因为nsstring和您的值可以是包装在适用的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.
我想关键是你有很多选择。如果我是你,我会做一些实验,看看什么最有效。
#4
2
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和绑定。
#5
0
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和objective - 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
这里的问题解决了
#1
44
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];
#2
20
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);
}
#3
11
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基本上就是对象的结构体。您的键可以是结构组件的名称,因为nsstring和您的值可以是包装在适用的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.
我想关键是你有很多选择。如果我是你,我会做一些实验,看看什么最有效。
#4
2
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和绑定。
#5
0
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和objective - 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
这里的问题解决了