如何在C ++中创建Cocoa对象? [英] How to create Cocoa objects in C++?
问题描述
有可能和如何创建类的对象,如NSString从C ++代码?
Is it possible and how to create objects of such classes like NSString from C++ code?
推荐答案
选项:
1)如果你主要是用C ++编写的,想要与一个期望可可字符串和集合的API进行通信,那么Core Foundation是一个C C ++)api用于创建和交互许多Foundation的类。
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)如果你将c ++代码和objective-c代码集成在一起,那么,你可以选择将接口类编译为objective-c ++,它允许你在同一个源文件中混合。因此,有效的是做 vector.push_back([NSString stringWithFormat:@string])
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 )通过包括< objc / runtime.h>
,可以访问(接近)整个目标c语言作为C运行时库。我不能真正想到一个很好的理由,使用这一个其他2,但如果有一个很好的理由,你不能这是值得铭记。
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.
这篇关于如何在C ++中创建Cocoa对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!