开发者

NSArray of structures?

开发者 https://www.devze.com 2023-03-19 16:34 出处:网络
Trying to implement the following structure from \"c\" to use NSArray in objective-c: In standard-c: struct structDog{

Trying to implement the following structure from "c" to use NSArray in objective-c:

In standard-c:

struct structDog{
    char *name;
    int age;
};

struct structLitter{
    struct structDog puppy[10];
};

Then I use malloc to allocate space. But since I am using NSArray.

But in Objective-c I am using NSArray... so ???

开发者_如何学Go
NSArray struct structDog *puppy; // <<---this doesn't work

thanks


Assuming that you are trying to do is get your struct into your NSArray you need to use NSValue. For instance you can do something like:

NSArray* myArray = [NSArray arrayWithObjects:[NSValue valueWithPointer: myDog],
                                             [NSValue valueWithPointer: myPuppy],
                                             nil];

structDog* dog = (structDog*)[[myArray objectAtIndex:0] pointerValue]; 
0

精彩评论

暂无评论...
验证码 换一张
取 消