是否可以用 C++ 对 iPhone 进行编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/270455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to program iPhone in C++
提问by Dmitri Nesteruk
I'm all for language diversity, but Objective C is insane. So I'm curious: is it possible to code iPhone apps with C++ while using the Cocoa API, etc?
我完全支持语言多样性,但 Objective C 太疯狂了。所以我很好奇:是否可以在使用 Cocoa API 等的同时使用 C++ 编写 iPhone 应用程序?
采纳答案by Robert S.
Short answer, yes, sort of. You can use Objective-C++, which you can read about at Apple Developer Connection.
简短的回答,是的,有点。您可以使用 Objective-C++,您可以在Apple Developer Connection 上阅读相关内容。
If you know C++ already, learning Objective-C would be pretty simple, if you decided to give that a try. More info on that topic is at the ADC as well.
如果您已经了解 C++,那么如果您决定尝试一下,学习 Objective-C 将非常简单。关于该主题的更多信息也在 ADC 中。
回答by Mitch Haile
Although Objective-C does indeed appear to be "insane" initially, I encourage you to stick with it. Once you have an "a-ha" moment, suddenly it all starts to make sense. For me it took about 2 weeks of focused Objective-C concentration to really understand the Cocoa frameworks, the language, and how it all fits together. But once I really "got" it, it was very very exciting.
尽管 Objective-C 一开始确实看起来很“疯狂”,但我鼓励你坚持下去。一旦你有一个“啊哈”的时刻,突然间这一切都开始变得有意义了。对我来说,我花了大约 2 周的时间专注于 Objective-C 才真正理解 Cocoa 框架、语言以及它们如何组合在一起。但是一旦我真的“得到”了它,那是非常非常令人兴奋的。
It sounds cliché, but it's true. Stick it out.
这听起来很陈词滥调,但这是真的。坚持到底。
Of course, if you're bringing in C++ libraries or existing C++ code, you can use those modules with Objective-C/Objective-C++.
当然,如果您要引入 C++ 库或现有的 C++ 代码,则可以将这些模块与 Objective-C/Objective-C++ 一起使用。
回答by Martin York
I use Objective-C to slap the UI together.
But the hard guts of the code is still written in C++.
我使用 Objective-C 将 UI 组合在一起。
但是代码的硬骨头仍然是用 C++ 编写的。
That is the main purpose of Objective-C the UI interface and handling the events.
And it works great for that purpose.
这是Objective-C UI界面和处理事件的主要目的。
它非常适合这个目的。
I still like C++ as the backend for the code though (but that's mainly becuase I like C++) you could quite easily use Objective-C for the backend of the application as well.
我仍然喜欢 C++ 作为代码的后端(但这主要是因为我喜欢 C++)你也可以很容易地将 Objective-C 用于应用程序的后端。
回答by dnolen
First off, saying Objective-C is "insane" is humorous- I have the Bjarne Stroustrup C++ book sitting by my side which clocks in at 1020 pages. Apple's PDF on Objective-C is 141.
首先,说 Objective-C 是“疯狂的”是很幽默的——我身边有一本 Bjarne Stroustrup C++ 书,有 1020 页。Apple 在 Objective-C 上的 PDF 是 141。
If you want to use UIKit it will be very, very difficult for you to do anything in C++. Any serious iPhone app that conforms to Apple's UI will need it's UI portions to be written in Objective-C. Only if you're writing an OpenGL game can you stick almost entirely to C/C++.
如果你想使用 UIKit,那么你在 C++ 中做任何事情都会非常非常困难。任何符合 Apple UI 的严肃 iPhone 应用程序都需要用 Objective-C 编写它的 UI 部分。只有在编写 OpenGL 游戏时,您才能几乎完全坚持使用 C/C++。
回答by Chris Jefferson
Having some experience of this, you can indeed use C++ code for your "core" code, but you have to use objective-C for anything iPhone specific.
有了这方面的一些经验,您确实可以将 C++ 代码用于“核心”代码,但是对于 iPhone 特定的任何内容,您都必须使用 Objective-C。
Don't try to force Objective-C to act like C++. At first it will seem to you this is possible, but the resulting code really won't work well with Cocoa, and you will get very confused as to what is going on. Take the time to learn properly, without any C++ around, how to build GUIs and iPhone applications, then link in your C++ base.
不要试图强迫 Objective-C 表现得像 C++。乍一看,您认为这是可能的,但是生成的代码在 Cocoa 中确实不能很好地工作,并且您会对正在发生的事情感到非常困惑。花时间在没有任何 C++ 的情况下正确学习如何构建 GUI 和 iPhone 应用程序,然后链接到您的 C++ 基础。
回答by Frederik Slijkerman
You have to use Objective C to interface with the Cocoa API, so there is no choice. Of course, you can use as much C++ as you like behind the scenes (Objective C++ makes this easy).
您必须使用 Objective C 来与 Cocoa API 交互,因此别无选择。当然,您可以在幕后使用尽可能多的 C++(Objective C++ 使这变得容易)。
It is an insane language indeed, but it's also... kind of fun to use once you're a bit used to it. :-)
它确实是一种疯狂的语言,但它也......一旦你习惯了它就会很有趣。:-)
回答by goblinjuice
I'm not sure about C++, but you can definitely code iPhone applications in C#, using a product called MonoTouch.
我不确定 C++,但您绝对可以使用名为MonoTouch的产品在 C# 中编写 iPhone 应用程序。
You can see this post for detailed discussion on MonoTouch Vs Obj-C: How to decide between MonoTouch and Objective-C?
您可以查看此帖子以详细讨论 MonoTouch 与 Obj-C:如何在 MonoTouch 和 Objective-C 之间做出决定?
回答by JulianSymes
I'm in the process of porting a computation-intensive Android app written in Java to iOS6. I'm doing this by porting the non-UI parts from Java to C++, writing the (minimal) UI parts in Obj-C, and wrapping the former in a (small) C interface using the standard C/C++ technique, so that it can be accessed from Obj-C, which is after all a superset of C.
我正在将用 Java 编写的计算密集型 Android 应用程序移植到 iOS6。我这样做是通过将非 UI 部分从 Java 移植到 C++,在 Obj-C 中编写(最小)UI 部分,并使用标准 C/C++ 技术将前者包装在(小)C 接口中,以便它可以从 Obj-C 访问,它毕竟是 C 的超集。
This has been effective so far, and I haven't encountered any gotchas. It seems to be a legitimate approach, since Xcode lets you create C++ classes as well as Obj-C classes, and some of the official sample code does things this way. I haven't had to go outside any officially supported interfaces to do this.
到目前为止,这一直是有效的,我还没有遇到任何问题。这似乎是一种合法的方法,因为 Xcode 允许您创建 C++ 类以及 Obj-C 类,并且一些官方示例代码就是这样做的。我不必离开任何官方支持的接口来执行此操作。
There wouldn't seem to be much to gain from writing my remaining UI code in C++ even if it were possible, in view of the help given to you by the interface builder in Xcode, so my answer would be that you can use C++ for almost all your app, or as much of it as you find appropriate/convenient.
考虑到 Xcode 中的界面构建器为您提供的帮助,即使可能,用 C++ 编写我剩余的 UI 代码似乎也没有什么好处,所以我的答案是您可以使用 C++几乎所有您的应用程序,或者您认为合适/方便的应用程序。
回答by Bilal Ahsan
Yes but Thinking that you can program every kind of program in a single language is a flawed idea unless you are writing very simple programs. Objective C is for Cocoa as C# is for .NET, Use the right tool for right job, Trying to make C++ interact to Cocoa via writing bridging code and trying to make C++ code behave according to Cocoa requirements is not a good idea neither expecting C++ performance from Objective C is. You should try to layout design and architecture of app keeping in view existing skills and determine which part should be written in which language then build accordingly.
是的,但是除非您正在编写非常简单的程序,否则认为您可以用一种语言编写各种程序是一个有缺陷的想法。目标 C 适用于 Cocoa,就像 C# 适用于 .NET,使用正确的工具做正确的工作,尝试通过编写桥接代码使 C++ 与 Cocoa 交互并尝试使 C++ 代码按照 Cocoa 的要求运行并不是一个好主意,也不期望 C++目标 C 的表现是。您应该尝试根据现有技能来布局应用程序的设计和架构,并确定应该用哪种语言编写哪个部分,然后相应地构建。
回答by Arpad Goretity
I'm currently writing an Objective-C++ framework called Objective-X, wich makes PURE C++ iPHONE PROGRAMMING possible. You can do like this:
我目前正在编写一个名为 Objective-X 的 Objective-C++ 框架,这使得 PURE C++ iPHONE PROGRAMMING 成为可能。你可以这样做:
#import?"ObjectiveX.h"
void?GUIApplicationMain()?{
????GUIAlert?Alert;
????GUILabel?Label;
????GUIScreen?MainScreen;
????Alert.set_text(@"Just?a?lovely?alert?box!");
????Alert.set_title(@"Hello!");
????Alert.set_button(@"Okay");
????Alert.show();
????Label.set_text(@"Ciao!");
????Label.set_position(100,?200,?120,?40);
????MainScreen.init();
????MainScreen.addGUIControl(Label.init());
}
and compile it using GCC's appropriate commandline options. I've already compiled this helloworld app&it w0rkX0rz like a charm. ;-) It'll available soon on GoogleCode. Search for Objective-X or visit http://infotronix.orgfree.com/objectivexapprox. a week later!
并使用 GCC 的适当命令行选项编译它。我已经像魅力一样编译了这个 helloworld 应用程序和它 w0rkX0rz。;-) 它将很快在 GoogleCode 上提供。搜索 Objective-X 或访问http://infotronix.orgfree.com/objectivex大约。一周之后!
Updated (but apparently inactive) URL: http://code.google.com/p/objectivex/
更新(但显然无效)网址:http: //code.google.com/p/objectivex/