0

Is it possible and how to create objects of such classes like NSString from C++ code?

1 Answer 1

2

You've got 3 options:

1) If you're mainly writing in C++ and want to communicate with an API that expects cocoa strings and collections, then Core Foundation is a C (and therefore C++) api for creating and interacting with many of Foundation's classes. Many NS-prefixed cocoa classes have a CF-prefixed equivalent, and can be used interchangeably.

2) If you're integrating c++ code with objective-c code, then you have the option of compiling the interface classes as objective-c++ which allows you to mix both in the same source file. So it's valid to do stuff like vector.push_back( [NSString stringWithFormat:@"string"] )

3) It is possible to access (near enough) the whole objective-c language as a C runtime library by including <objc/runtime.h>. I can't actually think of a good reason for using this over one of the other 2, but it might be worth keeping in mind if there's a good reason you can't.

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

2 Comments

thanks, can you please tell about #2, how do I compile files as objective-c++?
The simple solution is to change your source files extension to .mm; slightly less drastic is to select your C++ source file in Xcode and change it's compiler option to ObjC++ (via the inspector)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.