Wednesday, February 25, 2009
Variable-length array of primitives in Objective-C
You won't always know the size you need to allocate for an array at compile time and using any more memory than necessary by allocating more than you'll ever need is out of the question for an embedded system. If only we had the same kind of constructs that are available in C++ or we were working with a superset of C... oh right - we are! The Foundation library provides an NSMutableArray class but it only works with objects. No worry, just wrap your primitive(s) with a custom class that inherits from NSObject and you're set. :) Of course there is some overhead to working with objects but not nearly as much as allocating an exorbitant amount of array indices.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment