ARC下的id数组成员实例 [英] id array member instance under ARC

查看:71
本文介绍了ARC下的id数组成员实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写这样的东西:

@interface Foo{

    __strong id idArray[]; 

}
@end

但是编译器对此抱怨:

Field has incomplete type '__strong id []'.

如何在ARC下创建id数组成员实例?以及如何初始化该数组? 使用malloc?新的[]?

How can I create an id array member instance under ARC? And how do I init that array? Using malloc? new[]?

我不想使用NSArray,因为我正在将大型库转换为ARC,这会导致很多工作.

I don't want to use NSArray because I'm converting a large library to ARC and that will cause a lot of work.

推荐答案

如果要动态分配数组,请使用ID为__strong的指针类型.

If you want to allocate dynamically the array, use pointer type of id __strong.

@interface Foo
{
    id __strong *idArray;
}
@end

使用calloc分配数组. __strong的ID必须以零开头.

Allocate the array using calloc. id __strong must be intialized with zero.

idArray = (id __strong *)calloc(sizeof(id), entries);

完成后,必须将nil设置为数组的项,然后释放.

When you are done, you must set nil to the entries of the array, and free.

for (int i = 0; i < entries; ++i)
    idArray[i] = nil;
free(idArray);

这篇关于ARC下的id数组成员实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆