在 Cocoa 中使用 C++ 而不是 Objective-C? [英] Use C++ with Cocoa Instead of Objective-C?

查看:43
本文介绍了在 Cocoa 中使用 C++ 而不是 Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写使用 C++ 和 Cocoa 框架的应用程序,因为 Apple 不支持 Carbon 64 位.C++ 在 Linux 和 Windows 上的实现似乎很普通,但在 Mac OS X 上,似乎需要额外的 Apple 特定代码(如 Obj-C 包装器).Apple 似乎也在强迫开发人员使用 Objective-C 而不是 C++ 来编写代码,尽管我可能是错的.

I would like to write applications that use C++ and the Cocoa frameworks because Apple is not making Carbon 64-bit capable. C++ seems to be pretty vanilla in its implementation on Linux and Windows but on Mac OS X it seems like additional Apple specific pieces of code are required (like an Obj-C wrapper). It also seems that Apple is forcing developers to write in Objective-C rather than C++, although I could be wrong.

我正在尝试寻找一种在 Mac 上编写代码的途径,以便于保持跨平台.必须用 C++ 为 Linux/Windows 编写代码,然后用 Objective-C 重写大部分代码会非常低效.

I am trying to find a path to write code on the Mac that would be easy to keep cross platform. Having to write code in C++ for Linux/Windows and then rewrite large portions in Objective-C would be very inefficient.

有没有办法用 C++ 编写代码,以便将来支持并在 Xcode 中支持?另外,如果这是可能的,我将如何在 Xcode 中混合使用 C++ 和 Objective-C?谢谢.

Is there a way to write code in C++ that will be supported for the future and supported in Xcode? Also, if this is possible, how would I mix C++ and Objective-C in Xcode? Thanks.

推荐答案

您不能完全用 C++ 编写 Cocoa 应用程序.Cocoa 的许多核心技术(例如键值绑定、委托(Cocoa 风格)和目标-动作模式)在很大程度上依赖于 Objective-C 的后期绑定功能.后期绑定要求使得在编译时绑定的类型语言(如 C++ⁱ)中实现 Cocoa API 变得非常.当然,您可以编写在 OS X 上运行的纯 C++ 应用程序.它只是不能使用 Cocoa API.

You cannot write a Cocoa application entirely in C++. Cocoa relies heavily on the late binding capabilities of Objective-C for many of its core technologies such as Key-Value Bindings, delegates (Cocoa style), and the target-action pattern. The late binding requirements make it very difficult to implement the Cocoa API in a compile-time bound, typed language like C++ⁱ. You can, of course, write a pure C++ app that runs on OS X. It just can't use the Cocoa APIs.

因此,如果您想在其他平台上的 C++ 应用程序和基于 Cocoa 的应用程序之间共享代码,您有两种选择.首先是用C++写模型层,用Cocoa写GUI.这是一些非常大的应用程序常用的方法,包括 Mathematica.你的 C++ 代码可以保持不变(你不需要时髦的"苹果扩展来在 OS X 上编写或编译 C++).您的控制器层可能会使用 Objective-C++(可能是您所指的时髦的"Apple 扩展).Objective-C++是C++的超集,就像Objective-C是C的超集一样.在Objective-C++中,你可以进行objc风格的消息传递调用(如[some-objc-object callMethod];) 来自 C++ 函数.相反,您可以从 ObjC 代码中调用 C++ 函数,例如:

So, you have two options if you want to share code between C++ apps on other platforms and your Cocoa-based application. The first is to write the model layer in C++ and the GUI in Cocoa. This is a common approach used by some very large apps, including Mathematica. Your C++ code can be left unchanged (you do not need "funky" apple extensions to write or compile C++ on OS X). Your controller layer will likely make use of Objective-C++ (perhaps the "funky" Apple extension you refer to). Objective-C++ is a superset of C++, just as Objective-C is a superset of C. In Objective-C++, you can make objc-style message passing calls (like [some-objc-object callMethod];) from within a C++ function. Conversely, you can call C++ functions from within ObjC code like:

@interface MyClass {
    MyCPPClass *cppInstance;
}
@end

@implementation MyClass
- (id)init {
    if(self = [super init]) {
        cppInstance = new MyCPPClass();
    }
    return self;
}
- (void) dealloc {
    if(cppInstance != NULL) delete cppInstance;
    [super dealloc];
}
- (void)callCpp {
    cppInstance->SomeMethod();
}
@end

您可以在 Objective-C 语言中找到有关 Objective-C++ 的更多信息 指南.视图层可以是纯 Objective-C.

You can find out more about Objective-C++ in the Objective-C language guide. The view layer can then be pure Objective-C.

第二种选择是使用跨平台的 C++ 工具包.Qt 工具包可能符合要求.跨平台工具包通常被 Mac 用户鄙视,因为它们没有完全正确地获得所有外观和感觉细节,Mac 用户希望 Mac 应用程序的 UI 得到改进.然而,Qt 的表现出奇地好,根据受众和应用的使用情况,它可能已经足够好了.此外,您将失去一些特定于 OS X 的技术,例如 Core Animation 和一些 QuickTime 功能,尽管 Qt API 中有一些近似替代品.正如您所指出的,Carbon 不会移植到 64 位.由于 Qt 是在 Carbon API 上实现的,Trolltech/Nokia 不得不将 Qt 移植到 Cocoa API 以使其与 64 位兼容.我的理解是 Qt 的下一个版本(目前在 release candiate) 完成此转换,并且在 OS X 上兼容 64 位.如果您对集成 C++ 和 Cocoa API 感兴趣,您可能需要查看 Qt 4.5 的源代码.

The second option is to use a cross-platform C++ toolkit. The Qt toolkit might fit the bill. Cross-platform toolkits are generally despised by Mac users because they do not get all the look and feel details exactly right and Mac users expect polish in the UI of Mac applications. Qt does a surprisingly good job, however, and depending on the audience and the use of your app, it may be good enough. In addition, you will lose out on some of the OS X-specific technologies such as Core Animation and some QuickTime functionality, though there are approximate replacements in the Qt API. As you point out, Carbon will not be ported to 64-bit. Since Qt is implemented on Carbon APIs, Trolltech/Nokia have had to port Qt to the Cocoa API to make it 64-bit compatible. My understanding is that the next relase of Qt (currently in release candiate) completes this transition and is 64-bit compatible on OS X. You may want to have a look at the source of Qt 4.5 if you're interested in integrating C++ and the Cocoa APIs.

ⁱ 有一段时间,Apple 将 Cocoa API 提供给 Java,但该桥接器需要大量的手动调整,并且无法处理更先进的技术,例如上述的键值绑定.当前,动态类型、运行时绑定语言(如 Python、Ruby 等)是编写没有 Objective-C 的 Cocoa 应用程序的唯一真正选择(当然,这些桥接器在幕后使用了 Objective-C).

ⁱ For a while Apple made the Cocoa API available to Java, but the bridge required extensive hand-tuning and was unable to handle the more advanced technologies such as Key-Value Bindings described above. Currently dynamically typed, runtime-bound languages like Python, Ruby, etc. are the only real option for writing a Cocoa app without Objective-C (though of course these bridges use Objective-C under the hood).

这篇关于在 Cocoa 中使用 C++ 而不是 Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆