开发者

object array for user defined class in objective-c?

hai folks, i want to create array for user defined class like in java. eg in java:

ClassA[] obj=new ClassA[10];

like this i want to make开发者_开发知识库 a array in objective-c, and also i want to return this object in the method.

eg in java:

ClassA[] method1()
{
     ClassA[] classA=new ClassA[10];

   return classA;
}

is it possible to do this one, without using NSArray. if it possible, how can i do this one in objective-c.

plz give me some sample code snippet for this. thanks in advance.


There is rarely a reason to avoid NSArray. However, if you are absolutely certain that you have one of those cases, then use the C idiom, because Obj-C is a superset of C.


You can do it in C style:

ClassA *objCollection[10];
objCollection = (ClassA *)malloc(sizeof(ClassA)*10);
objCollection[0] = [[ClassA alloc] init];
objCollection[1] = [[ClassA alloc] init];
...

In this way you are declaring a array of pointers to ClassA objects of size 10.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜