Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when should I use C/C++ code in iOS application?

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.

like image 519
Alex Bush Avatar asked Nov 02 '11 01:11

Alex Bush


People also ask

Why is Objective-C used for iOS?

It's because Objective C has been the de facto language for Mac OS X development before it was Mac OS X. When Jobs left Apple to set up NeXT, the language Objective C was developed as a specific language that wasn't C++ and avoided many of its pitfalls.

Why does Apple use C?

Apple will ditch the Lightning connector on its iPhones, the company has confirmed, after European regulators decided all smartphones should have USB charging as standard in two years' time.

Can you run C code on iOS?

XCode is compatible with C, C++ and Objective C as well as Swift. Objective C is based on C. You can execute any C program in XCode as long as it does not have any platform specific dependencies that would prevent it from running on an Apple device / computer.

Does iOS still use Objective-C?

Most of the core iOS and MacOs software is still written in Objective-C, though Apple is pushing for new updates to be written in Swift.


3 Answers

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.

like image 173
hotpaw2 Avatar answered Sep 24 '22 06:09

hotpaw2


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.

like image 29
Matt Wilding Avatar answered Sep 25 '22 06:09

Matt Wilding


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.

like image 29
Jeremy Friesner Avatar answered Sep 23 '22 06:09

Jeremy Friesner