开发者

Array Allocation in Objective C++

I want to declare an array of numbers that is flexible in size in an obj C file

I am doing this:

long * arr = NULL;

a[0] = 0;

but this is giving bad_excess error

Can anyone开发者_JAVA技巧 help me out

also

long *arr = malloc(sizeof(long));

doesn't seem to help either


Why don't you do something like this,

NSMutableArray *numArray = [[NSMutableArray alloc] init];

This creates a array of objects. You can add any number of objects to it. Here you want to add long values. So use the following code,

[numArray addObject:[NSNumber numberWithLong:37];
[numArray addObject:[NSNumber numberWithLong:45];
[numArray addObject:[NSNumber numberWithLong:12];

NSMutableArray is flexible in size as you expected. You can add any number of objects. If you want to get the number, you can do like the following,

long num1 = [[numArray objectAtIndex:0] longValue];
long num2 = [[numArray objectAtIndex:1] longValue];

You can also do as specified in the following link,

How to declare an array of floats as a class variable in Objective-C when the dimension is undefined at the class instantiation time?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜