Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Please forgive the humor of the title but it certainly gets my point across. I am quite familiar with C code, I can write it in my sleep and after reading through a few tutorials on iPhone development and Objective-C, I'm beginning to feel like I'd rather avoid Obj-C at all costs.

It's not that it's unlearnable or "hard". I just don't like it and after reading through many of the Pros and Cons of Objective-C vs. straight C, I've determined that if I need to write an iPhone App, I want to write as much of it as C as is possible.

With that said, what is the minimum amount of Objective-C that I need to be aware of or potentially write to be able to create an iPhone app?

If I can't write an app 100% in C, then is there a good reference that discusses writing iPhone Apps in C and only discusses the minimal amount of Objective-C logic that needs to be written to get an application to compile and run?

Also, is there any good reason why I should learn Objective-C if I don't have to use it for a C project? Specifically, are there times when Objective-C may provide a more optimal coding solution to a problem because of the way the compiler compiles and link the code? I may be a C advocate but more important than that I want to be sure that my application runs as smooth and optimally as possible.

Disclaimer

I am at an early learning stage of a project I may have to work on in the near future. If this process comes to fruition, I'll be getting a Mac as well as a developer license so I'm completely in ignorance. Even worse, if this project is determined to be feasible, I will then be getting the necessary equipment to develop for the iPhone. For now, I'm trying to learn as much information as I can and explore my options.

I'd appreciate any (constructive) thoughts, tips and suggestions.

share|improve this question

migrated from stackoverflow.com Aug 12 '11 at 21:42

This question came from our site for professional and enthusiast programmers.

add comment

7 Answers 7

up vote 6 down vote accepted

All of the view related classes supplied by the system are Objective-C, and they're generally written to expect to communicate with other Objective-C objects (though that's not as true as it was since blocks were added in iOS 4). However, Objective-C is a strict superset of C and all C code is directly callable. C code can be directly included in Objective-C source files, with the effect that C source can make Objective-C calls as necessary. So to remain neat I'd say that to build an application with the minimum amount of Objective-C, you should stick with model-view-controller, implement the view and controller in Objective-C and the the model in C.

The primary differences between Objective-C and C are the usual object oriented versus procedural differences plus the fact that the former is fully reflective, which in practice tends to bring efficiency savings in coding because of the permissible implementation patterns it opens up, such as target/action or key-value observing. The way Apple's frameworks are built up, to rest on ideas like delegation, also provide implementation efficiency gains over common C practices.

As hinted by Noah, all Objective-C tasks are achieved by C function calls, as documented in the Objective-C Runtime Reference. If you really wanted to get messy you could create Objective-C classes at runtime, supply the C functions you want to be linked to each method and then talk to Cocoa like that. It'd be unlikely to be syntactically pleasing, but the option is there.

share|improve this answer
add comment

With that said, what is the minimum amount of Objective-C that I need to be aware of or potentially write to be able to create an iPhone app?

Technically, none. You can replace any [foo someMethod:bar] with a call to the C function objc_msgSend and its variants, as in objc_msgSend(foo, @selector(someMethod:), bar) (more info on that here). It’ll make your code enormously more verbose, though, and as many of the frameworks on the OS—including almost the entirety of UIKit, the framework responsible for most user interaction and all of the standard UI—are written with Obj-C in mind, you’ll find yourself doing a lot of extra typing if you want to avoid using the standard syntax.

share|improve this answer
add comment

is there any good reason why I should learn Objective-C if I don't have to use it for a C project? Specifically, are there times when Objective-C may provide a more optimal coding solution to a problem because of the way the compiler compiles and link the code?

The angle that Objective C has above C: just think in terms of messaging which is built into the language. Implementing multithreaded POSIX (pthread) applications in C uses lots of message passing (or sockets; rarely signals). With ObjC this is just "built in" as a language construct and instead of making direct function calls to the same-context library or direct function calls to message senders which put a message into a queue, in ObjC a message is sent to the "library" with one line of code. As part of the language, this also ensures the communication method is standardized, clean, and difficult to do improperly. Really that's the major difference. The minor difference is in creating automatic variables for the data-structures-cum-objects defined by your app (or your library), and handling strings as objects using @ (which is a great language improvement, one which C has always needed for implementing layer4 and above).

The bigger issue of course is learning the thousands of new "function calls".

share|improve this answer
add comment

I'll answer the parts I can from experience.

I come from a similar background being a primarily C developer. When I started Objective-C, it seemed a little clunky and abstract. However, now that I've gotten into the rhythm of things, it's going pretty well.

You can code pretty much everything you want in C coding style.

[class function:params]      
class.function(params)

It doesn't really make a difference.

However, since you're doing iPhone development, you'll need to learn the syntax anyways in order to understand example code and read through documentation. So the way I saw it when I started, was why not just use the syntax they suggest and learn it as you go? That way, when you come across example code, it's more obvious what's going on.

I wouldn't personally use Objective-C for anything non-iPhone/OS-X related. But that's just my own preference.

share|improve this answer
add comment

Look into some of the OpenGL game examples, where almost an entire huge app (Wolfenstein, Doom, et.al.) can be coded in C, except for an initial view set-up and some UI event handling delegate stubs (maybe around a couple pages worth).

Even the latter can be done in straight C if you don't mind being much more verbose, by calling the UI APIs via the messaging runtime interface (see Noah's answer).

All of the example code and support is for using the UI APIs via Objective C, so you may end up having to write a ton more of your own custom UI code (as games sometimes do).

share|improve this answer
add comment

To write native Cocoa Touch apps, I'm not aware of any workaround Objective-C.
If you are going to develop iOS games however, you can enjoy C/C++ coding but you still need to write some Objective-C code to launch the app among other things.

share|improve this answer
add comment

If I can't write an app 100% in C, then is there a good reference that discusses writing iPhone Apps in C and only discusses the minimal amount of Objective-C logic that needs to be written to get an application to compile and run?

iPhone Games Projects has a chapter titled "Rapid Game Development Using (Mostly) Standard C".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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