Thoughts from a NeXTStep Guy on Cocoa Development

All the C you need to know?

Feb 27, 2012 by Bill Dudney

What do you find confusing about the C part of Objective-C?

As I'm preparing for the class I'm teaching with Daniel, I keep thinking about all the C stuff that I see people struggling with. I'd love to be able to do a mind meld and transfer a grok of C, but unfortunately it doesn't work that way.

And besides you don't really have to know C to be a competent iOS/Mac developer. For the most part you work in objects from UIKit, AppKit and Foundation. Only every once in a while do the quirks of C come up.

For example, you have to pass an NSError ** to methods that return an error. You don't really have to understand that C is "pass by copy" to use the pattern:

NSError *err = nil;
NSString *stuff = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];

You must pass the address of err to change the value of err in the current scope. You can build and deliver apps not understanding why. You just do what works and go on with solving real problems. After all we have to stay calm and ship apps.

But if you are like me, you can only do that for so long before it starts to make you nuts that you don't really understand it!

In the current edition of our book Chris does a great job of going over the stuff you need to know about C to become a proficient iOS developer. But iOS proficiency is the goal. We did not have the space to dig into the details of how pointers work or what "pass by copy" really means.

In addition to preparing for the studio, I've also been doing some web programming with HTML5 (HTML, CSS3 and JavaScript). I'm on that side of the divide where I'm 'copy and paste programming'. I see something on a blog, copy and paste it into my document, and only marginally understand it. But it works so I go on with solving the actual problem instead of taking the time to really 'get it'. I know exactly how it feels to be in that spot, and I hate it! :)

Which brings me to the point of this post. What do you find confusing or not understand with the C part of Objective-C?

Here is a list of topics I know I want to address:

  • pointers
  • function call semantics
  • extended types, struct, typedef, enum and #define
  • ObjC Strings with @"" vs C Strings with ""
  • dynamic memory & malloc/free
  • Core Foundation

Some of those items are abstractions of real world problems I've seen people struggle with, but I'm sure there are lots of others. Please send me an email with what you've struggled with and I'll do my best to address that as well.

If you are coming to ObjC from a scripting language, Scott Stevenson has a great tutorial on several C concepts that you might find useful... Very good stuff.


Bit flags might be helpful for things like NSCell and NSWindow, which use them. I've also found that people get very confused with Cocoa's event handling, because it hides its main event loop so well. So NSRunLoop/-sendEvent: may be worth looking at more closely.

I'd also like to shamelessly plug my own C tutorial,, where I tried to describe everything I think someone programming ObjC with Cocoa needs to know.

Posted by Uli Kusterer on February 27, 2012 at 08:05 AM MST #

if I may add my 2 cents. sometimes, using unity3d, if you want a native functionality like game center, gamekit, accelerometer data, you will need to wrap your methods in "C" extern functions. So if you plan to enter in the game development market you should learn a little bit of C.

Posted by Bruno on February 27, 2012 at 08:05 AM MST #

C arrays. They always manage to trip me up somehow. Cover good practice regarding checking your array indices. Also, different ways of making a dynamically sized multi-dimensional array: for example, I think this one requires C99 or something, so isn't always shown in tutorials:

BOOL (*map)[X][Y][Z] = (BOOL(*)[X][Y][Z])calloc(X*Y*Z, sizeof(BOOL));
(*map)[i][j][k] = NO;

Posted by Amy Worrall on February 27, 2012 at 08:28 AM MST #

Block/closure declarations might be worth discussing. I've seen lots of devs get confused with block declarations and block typedefs.

It maybe by worth discussing function pointers at the same time. However, the only practical use I can think of for function pointers in iOS is checking for API availability.

Posted by Benedict Cohen on February 27, 2012 at 10:36 AM MST #

I have recently been getting into C programming for an embedded system and the biggest struggles I had are with the basic stuff like string handling, thinking in terms of character arrays, thinking about strcpy and strncpy and all those functions and bounds checking. So I would say the biggest help would be to highlight the differences between C and Objective-C/Cocoa. In particular explain exactly why some things are still needed to be done in pure C. What are the primary justifications for CoreFoundation? For example it was my understanding that the Address Book APIs in iOS, at least a couple years ago, were still basic C APIs. Why is that? Answering the basic question of when and why to use C vs the higher level abstractions of Cocoa and Objective-C would be a good start.

Posted by Gordon on February 29, 2012 at 07:38 AM MST #

Another element of C that took me awhile to figure out (and I'm not even sure I've fully figured it out) is when, in an objective-c class does it pay to do a static inline function.

Posted by Josh on March 02, 2012 at 03:04 PM MST #

Post a Comment:
  • HTML Syntax: Allowed