5

When and especially why should I switch from Objective-C to C or C++ when I develop on iOS platform. I'm sure I'm missing something but as far as I can see Apple wants developers to use Objective-C everywhere in Mac/iOS environment so why would I go with C/C++?

One note, though, I'm NOT talking about GAME DEVELOPMENT. In case of games I realize what are the advantages. I just don't get when I want to use C if I develop, for example, a client for a server or something like that (once again, not a game). All the classes I need already there and all of them in Objective-C.

6
  • 7
    I'll be flamed for this, but anyway, Objective-C isn't a well designed language to begin with, and nowhere close to C++ functionality-wise. So staying with C++ (if you know it) is a lot more beneficial in so many ways. Use Objective-C only when you have no choice. Nov 2, 2011 at 1:36
  • 5
    @GeneBushuyev Agreed that objective-C wasn't a well designed language to begin with, but neither was C++. (I seem to recall that objective C predates C++, but they were within a year or two of each other in the early 80s). Both languages have grown a lot since then, and gone in different directions. They're both now quite decent languages, but are for different purposes .. advising against using it unless you have no other choice seems a little extreme to me. Nov 2, 2011 at 1:50
  • Thanks everybody for the comments and answers. I thought I don't know some "very important super reason" to use one or another. Now it all makes more sense and more clear to me.
    – Alex Bush
    Nov 2, 2011 at 21:13
  • Now I just don't know which answer to pick as the right one :D
    – Alex Bush
    Nov 2, 2011 at 21:13
  • If I were you, I would choose hotpaw2's answer. It is the most thorough. Nov 6, 2011 at 18:15

4 Answers 4

4

Objective C is a strict superset of ANSI C, so an Objective C programmer is very likely already doing a significant amount of programming in C. In fact, many Mac and iOS APIs (audio, graphics, etc.) are C interfaces and/or use C data types.

A few of the reasons to do something in C, of things that are possible in both the superset and subset languages, might include performance and portability. There is a small amount of runtime overhead to Objective C messaging that is completely unsuitable for the inner loops of real-time audio or video image processing, etc. For portability, one can often use well encapsulated C code in iOS, Linux, Android NDKs, WebOS PDKs, & etc. And occasionally the Objective C wrappers for an OS service won't offer all the flexibility of some underlying C API.

The only reason I can see to use C++ for an iOS app might be to use some legacy C++ code and maintain some consistency of style with that code for readability, maintainability, etc.

For an iOS app where performance, or any portable code reuse is not important, there are few reasons to not just use Objective C (with its included C language), as that is what the most current documentation, tools, frameworks, and APIs for iOS are supported on.

4

The main advantage is portability. A chunk of code written in C++ or C can, when appropriate measures are taken, be very easily wrapped up and used in iOS or Android, or Blackberry, etc... Many 3rd party libraries go this route for that reason (Zebra Crossing and OpenCV come to mind immediately).

Another reason (and I would argue bad reason) is simply that a developer is comfortable in C or C++ and wants to avoid learning Objective-C as much as possible.

2

The only reasons I can think of are (a) you have an existing C++ codebase that you want to re-use (and don't want to have to rewrite it all in Objective C), or (b) you really like C++ and/or really don't like Objective C.

0

Why not mix and match as you like. The compiler supports "objective c++". Not sure that it supports C++11 features in such a mode, but it should support C++98 well. I think all that is required is that you change the naming of your source files from ".m" to ".mm". (This is all from memory and could be inaccurate).

As to the "why". Some apple APIs had more features in one language than the other. For example it used to be (and may still be) that a bunch of the graphics / quicktime functionality was only available through the cocoa interfaces, and not the objective C interfaces.

It is even more likely that some third party libraries may only be available in one language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.