Vectors in Obj-C

Mikuro

Crotchety UI Nitpicker
I'm pretty new to C and Cocoa. I'm coming mostly from REALbasic. Forgive me if this question is a little newbish.

I want to make an array that works more or less like REALbasic's. That is to say, I want to be able to dynamically resize the array and append new elements. NSArray isn't what I need, because it's only suitable for objects, and the "int myInt[10]" kind of array isn't it either, because as far as I can tell, there's no way to resize such arrays after their creation (PLEASE correct me if I'm wrong!)

I think what I need is a C++ vector. After much hair-pulling, I finally figured out how to create and use C++ vectors in my Objective-C code, following these surprisingly easy steps:

1. Rename my "*.m" file "*.mm" (yeesh! That took me WAY too long to figure out)
2. Add "#include <vector>" to the top of my .mm file.
3. Declare my vector like so: std::vector<NSRect> rectVector;

Great. The problem is, I can't figure out how to declare vectors in a .h file. It tells me "vector: No such file or directory", and gives me a "parse error before 'std'" (and a few warnings about C++ destructors).

I've tried changing the file type of my .h file using Xcode's info window (which is what renaming the .m file to .mm does automatically), but to no avail.


So, I have two questions:

1. Is there a native Objective-C analog of C++ vectors? It seems like there'd have to be, but...damned if I can find it.
2. If not, is it possible to use C++ vectors as part of an Objective-C class (i.e., in a .h file)?


What I'm trying to do specifically is create a list of NSRects that I can append to, and also a two-dimensional resizeable array of bools (though I can work around the 2D bool part easily enough, at least for now). I have no idea how big the list will need to be until runtime (could be anywhere from 0 to many thousands of elements). And it needs to be accessible from any method in its owning class.
 
C++ vectors use class templates so make sure you declare it correctly:
Code:
vector<dataType> test;
where dataType is "int" "double" "char" etc
 
I do #include <vector> in my .h file, but it tells me it can't find the file/directory. The same thing happened in my .m file before I changed it to .mm. I tried changing the file type of my .h file from sourcecode.c.h to various other cpp-based entries in Xcode's list, but I get the same two errors (and three warnings about constructors/destructors) no matter what.

It works in my .mm file perfectly, so I know I'm declaring it right.


One more question: I notice that all the examples I've seen don't use the "std::" in their vector declarations, but when I try it without the "std::", I get errors. Specifically:

error: `vector' undeclared (first use this function)
error: parse error before `>' token

Is there something I should be doing that would make it work without the std::?
 
Yes after your #includes there should be a line:
Code:
using namespace std;
But this isn't required, it's just helpful.
 
Okay, I feel stupid now.

The problem wasn't with my .h file at all. All my code was right. It all works.

The problem was that I was importing my .h file from other classes that used .m files, not .mm files. Renaming all of those to .mm fixed all the errors (I still get the warnings about constructors/destructors, but I think I can deal with that). All that stress over something as trivial as an improper file name extension. *sigh* Well, in the future I'll know better.

Thanks for the help, guys.
 
Back
Top