I want to use a C++ string object in my Objective-C code instead of a NSString so I don’t need to covert it (I need the std::string more often) and I tried doing it like this:
using namespace std;
@interface InstrumentGridViewController : UIViewController {
string* trackName; // also tried using std::string, didn't work
@property (nonatomic, assign) string* trackName;
}
I’m getting errors though, both the string* trackName; statement and the @property line give me Expected specifier-qualifier-list 'string'.
EDIT: I forgot to add #include <string> but adding this gives me the error String: no such file or directory
What matters is the source files this header file is included by. If this header file is included in a normal Objective-C source file (.m extension) the compiler will choke as you described. If you check the errors carefully in Xcode you can see exactly which source file or files this header file failed to compile in.
This is why it is often suggested never to use C++ objects in header files intended to be consumed by Objective C code. C++ in header files tends to bleed all over your code base and force you to compile everything was Objective C++ (.mm extension). Note that with the modern runtime and class extensions it is very easy to work around this. Just move your:
to a class extension in your InstrumentGridViewController.mm file and remove the
trackNameivar declaration that is not needed in the modern runtime. Then your header is Objective C clean and can be included by the rest of your code. Rob Napier has more discussion on this in Wrapping C++ Final Edition.