127

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?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166

11 Answers11

61

Short answer, yes, sort of. You can use Objective-C++, which you can read about at 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.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
Robert S.
  • 25,266
  • 14
  • 84
  • 116
  • 1
    can you recommend a website or book that is aimed at experienced C++ programmers to learn Objective-C? I managed to mash a few test apps together, but I find the syntax a little backwards and I am sure that I am doing things that lead to memory leaks etc just through lack of knowledge of Objective-C – Lea Hayes Jul 30 '11 at 14:26
  • 3
    @LeaHayes A free book called "From C++ to Objective-C". Assumes you know C++ and then tells you how things are done differently in Objective-C. http://pierre.chachatelier.fr/programmation/fichiers/cpp-objc-en.pdf (this is an English translation of the original French) – olafure Mar 31 '12 at 17:24
61

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.

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++.

Mitch Haile
  • 11,716
  • 2
  • 28
  • 25
43

I use Objective-C to slap the UI together.
But the hard guts of the code is still written in C++.

That is the main purpose of Objective-C the UI interface and handling the events.
And it works great for that purpose.

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.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 9
    @Spanky: How o I do what exactly. You can write normal C in *.m file. You can write normal C++ in *.mm files (Objective-C++). Inside Objective-C(++) files you can use normal `#include ` just like C/C++ files. – Martin York Jan 10 '13 at 08:14
28

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.

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++.

dnolen
  • 18,496
  • 4
  • 62
  • 71
  • 31
    Since when was less documentation a good thing? "The C++ Programming Language" is very thorough and also documents the STL. – Timmmm Apr 20 '10 at 08:46
  • 2
    @Timmmm: Ever since K&R made that point in the front matters of their book. Objective-C is a much smaller language than C++, with many of the same benefits (and some more of its own). Everything's tradeoffs, of course. But minimal documentation makes Obj-C pretty easy for experienced C (or C++) people to get up to speed with. – Ben Zotto Aug 09 '10 at 14:34
  • 65
    Sorry but ObjC is insane. The insistance on a completely different way of declaring functions, the use of brackets. It's being different for no benefit. They could have drafted the same effective language without the ridiculous syntax. They chose not to. Thus it's insane. – Rhubarb Oct 31 '10 at 13:08
  • 13
    @Rhubarb - Though I like Objective-C I have to agree that it's weird syntax serves no purpose. – Raphael Jun 26 '11 at 22:57
  • @Rhubarb, Yup, remember Apple is the company that came up with that insanity, and Apple is different for the sole purpose of being different. – SeanRamey Apr 06 '18 at 21:21
  • 2
    Objective-C and C++ came about at around the same time, both with the goal of adding object oriented programming to C. Objective-C followed Smalltalk’s model of everything being dynamic dispatch, C++ chose to use function pointers (virtual methods) basically. The weird syntax serves to differentiate ObjC message dispatching from regular C, direct function calls. Not sure if it was strictly necessary, but it’s one thing that makes it very easy to mix and match ObjC and C++ in Objective-C++. – Baxissimo Jun 26 '19 at 22:02
14

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.

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.

Chris Jefferson
  • 7,225
  • 11
  • 43
  • 66
3

I'm not sure about C++, but you can definitely code iPhone applications in C#, using a product called MonoTouch.

You can see this post for detailed discussion on MonoTouch Vs Obj-C: How to decide between MonoTouch and Objective-C?

Community
  • 1
  • 1
goblinjuice
  • 3,184
  • 24
  • 26
3

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).

It is an insane language indeed, but it's also... kind of fun to use once you're a bit used to it. :-)

Frederik Slijkerman
  • 6,471
  • 28
  • 39
2

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.

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.

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.

JulianSymes
  • 2,135
  • 22
  • 24
2

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.

Bilal Ahsan
  • 172
  • 1
  • 1
  • 9
0

It may be a bit offtopic, but anyway. You can program c++ right on iOS devices. Check out CppCode ios app - http://cppcode.info. I believe it helps to learn c and c++ and objective-c later.

4ntoine
  • 19,816
  • 21
  • 96
  • 220
0

I'm currently writing an Objective-C++ framework called Objective-X, wich makes PURE C++ iPHONE PROGRAMMING possible. You can do like this:

#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/objectivex approx. a week later!

Updated (but apparently inactive) URL: http://code.google.com/p/objectivex/

Community
  • 1
  • 1