Jay Zhao
Jay Zhao

Reputation: 956

id array member instance under ARC

I want to write something like this:

@interface Foo{

    __strong id idArray[]; 

}
@end

But the compiler complains about it:

Field has incomplete type '__strong id []'.

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

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

Upvotes: 5

Views: 2904

Answers (3)

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 13999

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

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

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

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

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);

Upvotes: 11

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

Either you give the array a fixed size:

__strong id idArray[20];

or you use a pointer and malloc:

__strong id *idArray;

...

self.idArray = calloc(sizeof(id), num);

Upvotes: 1

Georg Fritzsche
Georg Fritzsche

Reputation: 98994

You have to specify an array size, e.g.:

__strong id idArray[20]; 

Upvotes: 1

Related Questions