1

In other languages I could create a class then use this to create an array of objects in that class eg class price which is used in a performance class to define a price[] prices;

in objective C i cant get this to work, how do you do it?

The price class is an inherit of NSObject, can I use NSMutableArray?

2
  • I'm not quite sure what you're asking... can you rephrase? Are you saying you want an array of instances of the price class, or an NSArray that is an ivar of the price class... or something else? What exactly are you trying to accomplish that you can't? Commented Jun 24, 2009 at 16:22
  • And you say you can create a "custom array?" in other languages. As in C? If it's C/C++ could you give me a skeleton example of how you would do it there as well as clarifying your question, because as I said previously, I'm not quite sure what you're asking. Commented Jun 24, 2009 at 16:35

3 Answers 3

2

If you have a class Price, and it inherits from NSObject, then you can have an array of them stored in an NSArray or NSMutableArray. You could also store them in a C array, or an STL vector, although the memorymanagement sematics may be difficult in those cases.

In the case of an NSArray/NSMutableArray, the array takes an ownership reference on the object, so you can release it after adding it to the array, and it will remain in memory until it is removed from the array (and all other locations).

Code might look like:

NSMutableArray* a = [NSMutableArray array];
[a addObject:[Price price]];
// and/or
[a addObject:[[[Price alloc] init] autorelease];
// and/or
Price* p = [[Price alloc] init];
[a addObject:p];
[p release];
NSLog( @"The array is %@", a );
// Remember at this point you do not "own" a, retain it if you want to keep it, or use [NSMutableArray new] above

When a is released, all the Price objects it contains will be released (and potentially deallocated).

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, NSMutableArray is what you would want to use.

Comments

1

To answer your last question first: if you are storing instances of Objective-C object (which, as you say, inherit from NSObject) then yes, use NSMutableArray. However, this won't work for primitive types (like int, BOOL, char, char*, etc.).

Objective-C uses the C way of declaring arrays with one caveat: you can't allocate memory on the stack (which is what that does in C), only on the heap. That means you have to use malloc (prefer NSAllocateCollectable() on Leopard or later) and free to manage the memory. Also, you declare the array as a pointer to an array or pointers. Something like this: (Note: pulled from thin air, untested.)

Price **prices = malloc(numberOfEntries * sizeof(Price*));
...
free(prices);

Using an NSMutableArray is generally much easier than managing your own arrays. Just be aware of the memory management rules: collections call -retain on an object when it is inserted, and -release on it when it is removed (or on all objects if the collection is deallocated).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.