Objective-C 中用户定义类的对象数组?

发布于 2024-10-06 16:57:04 字数 384 浏览 0 评论 0原文

各位朋友们, 我想为用户定义的类创建数组,就像在java中一样。 例如在java中:

ClassA[] obj=new ClassA[10];

像这样我想在objective-c中创建一个数组,并且我想在方法中返回这个对象。

例如,在 java 中:

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

   return classA;
}

是否可以在不使用 NSArray 的情况下执行此操作。如果可能的话,我怎样才能在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-10-13 16:57:04

很少有理由避免使用 NSArray。但是,如果您绝对确定遇到其中一种情况,那么请使用 C 习惯用法,因为 Obj-C 是 C 的超集。

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.

我偏爱纯白色 2024-10-13 16:57:04

您可以用 C 风格来实现:

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

通过这种方式,您可以声明一个指向大小为 10 的 ClassA 对象的指针数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文