开发者

NSMutableArray of Structures?

开发者 https://www.devze.com 2023-03-20 07:15 出处:网络
How can I create NSMutableArray of Structures? I can create an array of Structures in standard-c but am running into problems 开发者_StackOverflow中文版in objective-c.

How can I create NSMutableArray of Structures?

I can create an array of Structures in standard-c but am running into problems 开发者_StackOverflow中文版in objective-c.

standard-c:

struct person people[10];

thanks


You need to copy each struct into an NSData or NSValue object in order to place it in an NSArray.

// in
struct person someGuy = ...;
NSData *personData = [NSData dataWithBytes:&someGuy length:sizeof(struct person)];
[personArray addObject:personData];

// out
NSData *personData = [personArray objectAtIndex:whatever];
struct person someGuy;
[personData getBytes:&someGuy];

You should understand the difference between stack and heap and how to work with pointers (or be ready to learn), otherwise you will see a lot of EXC_BAD_ACCESS (or worse, no exceptions, just mysterious garbage data).

0

精彩评论

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