You can create a CFMutableArray
instead which can handle arrays of arbitrary objects, and you can use it as you would an NSMutableArray
(for the most part).
// create the array
NSMutableArray *lstPack = (NSMutableArray *) CFArrayCreateMutable(NULL, 0, NULL);
// add an item
[lstPack addObject:pack];
// get an item
Pack *anObject = (Pack *) [lstPack objectAtIndex:0];
// don't forget to release
// (because we obtained it from a function with "Create" in its name)
[lstPack release];
The parameters to CFArrayCreateMutable
are:
- The allocator to use for the array. Providing
NULL
here means to use the default allocator.
- The limit on the size of the array. 0 means that there is no limit, any other integer means that the array is only created to hold exactly that many items or less.
- The last parameter is a pointer to a structure containing function pointers. More info can be found here. By providing
NULL
here, it means that you don't want the array to do anything with the values that you give it. Ordinarily for an NSMutableArray
, it would retain
objects that are added to it and release
objects that are removed from it¹, but a CFMutableArray
created with no callbacks will not do this.
¹ The reason that your code is failing is because the NSMutableArray
is trying to send retain
to your Pack
struct, but of course, it is not an Objective-C object, so it bombs out.