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

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

问题描述

我想编写使用C ++和Cocoa框架的应用程序,因为苹果并没有使得64位的能力。 C ++在Linux和Windows上的实现看起来很漂亮,但是在Mac OS X上,似乎需要额外的Apple特定的代码片段(如Obj-C包装器)。也似乎苹果正在迫使开发人员写的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 / 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中支持?此外,如果这是可能的,我将如何混合C ++和Objective-C在Xcode?感谢。

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很大程度上依赖于Objective-C的许多核心技术(例如键值绑定,代理(Cocoa风格)和目标操作模式)的后期绑定功能。后期绑定要求使得在编译时绑定的类型语言(如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] ; )。相反,你可以从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- 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用户鄙视,因为他们没有得到所有的L& F细节完全正确,Mac用户期望在Mac应用程序的UI中抛光。 Qt做了一个令人惊讶的好工作,然而,根据受众和应用程序的使用,它可能是足够好。此外,您将放弃一些特定于OS X的技术,例如Core Animation和一些QuickTime功能,尽管在Qt API中有大致的替换。正如你所指出的,Carbon不会移植到64位。由于Qt是在Carbon API上实现的,Trolltech / Nokia不得不将Qt移植到Cocoa API以使其64位兼容。我的理解是,Qt的下一个更新(目前在发布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 L&F 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 loose 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).

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

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