简体   繁体   English

用C ++开发iPhone应用程序?

[英]iPhone Application Development with C++?

I've written a few small programs in Objective-C (for the iPhone) but ultimately I want to write my programs mainly in C++. 我已经用Objective-C(用于iPhone)编写了一些小程序,但最终我想主要用C ++编写程序。 (I just find it a lot easier.) (我发现它要容易得多。)

If this is true, how would I: 如果是这样,我将如何:

  1. Manage memory in C++? 在C ++中管理内存? (Does C++ have a release-like command I need to use?) (C ++是否有我需要使用的类似发行版的命令?)
  2. Intermix C++ and Objective-C coding? 混用C ++和Objective-C编码? (Or even, should I?) (或者什至是我?)
  3. Take a C++ object, like a string, and convert it into an NSString? 拿一个像字符串一样的C ++对象,并将其转换为NSString吗?

Thank you! 谢谢! Derek 德里克

  1. Yes. 是。 C++ has a delete keyword, but it only applies to objects you've created with new (which, for idiomatic C++ code, is not every object). C ++有一个delete关键字,但它仅适用于用new创建的对象(对于惯用的C ++代码,并不是每个对象)。 C++ also doesn't have built-in reference counting, just pure ownership. C ++也没有内置的引用计数,只有纯所有权。

  2. If you make a source file with a .mm extension, it compiles as Objective-C++, which lets you intermix Objective-C and C++ code. 如果您使用扩展名为.mm的源文件,则它将作为Objective-C ++进行编译,从而可以将Objective-C和C ++代码混合在一起。

  3. For a string, you can call std::string::c_str() to get a string that you can pass into +[NSString stringWithUTF8String:] . 对于字符串,可以调用std::string::c_str()来获取可以传递给+[NSString stringWithUTF8String:]的字符串。

My two cents: if you feel that C++ is a lot easier than Objective-C and you don't know anything about memory management in C++, you should try to spend a fair amount of time learning pure C++; 我的两分钱:如果您觉得C ++比Objective-C容易得多,并且对C ++中的内存管理一无所知,则应该花大量时间学习纯C ++。 it's extremely easy to shoot yourself in the foot in C++ if you don't know what you're doing. 如果您不知道自己在做什么,用C ++射击自己是非常容易的。

Manage memory in C++? 在C ++中管理内存? (Does C++ have a release-like command I need to use?) (C ++是否有我需要使用的类似发行版的命令?)

c++ uses new and delete . c ++使用newdelete specifically, c++ programs prefer to use scope-bound-resource-management (SBRM). 特别是,c ++程序更喜欢使用范围绑定资源管理(SBRM)。 managing dynamic allocations is dead easy when you use these containers. 使用这些容器时,管理动态分配非常简单。 reference counting, however, is not currently built into the language -- you can use boost www.boost.org for more advanced pointer containers, including those which offer reference counting. 但是,引用计数当前尚未内置于该语言中,您可以将boost www.boost.org用于更高级的指针容器,包括那些提供引用计数的容器。

Intermix C++ and Objective-C coding? 混用C ++和Objective-C编码? (Or even, should I?) (或者什至是我?)

you can easy accomplish by using the extension .mm or .M , or by using a compiler flag. 您可以使用扩展名.mm.M或使用编译器标志轻松完成。 note that you should not just enable everything as objc++ -- this will hurt your build times. 请注意,您不应该仅将所有功能都启用为objc ++ -这会损害您的构建时间。 also note that there are a few restrictions, including the inability to subclass c++ types as objc types and vice-versa. 还应注意,存在一些限制,包括不能将c ++类型子类化为objc类型,反之亦然。 another important flag which any sane c++ dev would enable is the one which generates c++ constructor/destructor calls when you use c++ types as variables in your objc classes. 任何理智的c ++开发人员都会启用的另一个重要标志是,在将c ++类型用作objc类中的变量时,该标志会生成c ++构造函数/析构函数调用。 otherwise, you'll just crash, be forced to use pimpl, or have to manually construct/destruct all your c++ instances (as ivars in objc types). 否则,您将崩溃,被迫使用pimpl或必须手动构造/破坏所有c ++实例(如objc类型中的ivars)。 this means these types you use will all need default constructors. 这意味着您使用的这些类型都将需要默认构造函数。 you can intermix the languages, it's fine to do this if it is your preference. 您可以混合使用各种语言,如果您愿意,可以这样做。 there are a few more notes on mixing them in apple's docs, but those are the important ones... oh, and be careful to quarantine your exceptions (which you must also do with objc). 在苹果文档中还有一些关于混合它们的说明,但是这些是重要的...哦,请小心隔离您的异常(也必须对objc进行隔离)。

Take a C++ object, like a string, and convert it into an NSString? 拿一个像字符串一样的C ++对象,并将其转换为NSString吗?

see John Calsbeek's response 见约翰·卡尔斯贝克的回应

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM